diff --git a/src/github.com/matrix-org/go-neb/api.go b/src/github.com/matrix-org/go-neb/api.go index 2b68d58..6d63678 100644 --- a/src/github.com/matrix-org/go-neb/api.go +++ b/src/github.com/matrix-org/go-neb/api.go @@ -263,3 +263,38 @@ func (h *getServiceHandler) OnIncomingRequest(req *http.Request) (interface{}, * Config types.Service }{srv.ServiceID(), srv.ServiceType(), srv}, nil } + +type getSessionHandler struct { + db *database.ServiceDB +} + +func (h *getSessionHandler) OnIncomingRequest(req *http.Request) (interface{}, *errors.HTTPError) { + if req.Method != "GET" { + return nil, &errors.HTTPError{nil, "Unsupported Method", 405} + } + var body struct { + RealmID string + UserID string + } + if err := json.NewDecoder(req.Body).Decode(&body); err != nil { + return nil, &errors.HTTPError{err, "Error parsing request JSON", 400} + } + + if body.RealmID == "" || body.UserID == "" { + return nil, &errors.HTTPError{nil, `Must supply a "RealmID" and "UserID"`, 400} + } + + session, err := h.db.LoadAuthSessionByUser(body.RealmID, body.UserID) + if err != nil { + if err == sql.ErrNoRows { + return nil, &errors.HTTPError{err, `Session not found`, 404} + } + return nil, &errors.HTTPError{err, `Failed to load session`, 500} + } + + return &struct { + ID string + Authenticated bool + Session types.AuthSession + }{session.ID(), session.Authenticated(), session}, nil +} diff --git a/src/github.com/matrix-org/go-neb/goneb.go b/src/github.com/matrix-org/go-neb/goneb.go index 2e107bd..9a93067 100644 --- a/src/github.com/matrix-org/go-neb/goneb.go +++ b/src/github.com/matrix-org/go-neb/goneb.go @@ -41,6 +41,7 @@ func main() { http.Handle("/test", server.MakeJSONAPI(&heartbeatHandler{})) http.Handle("/admin/getService", server.MakeJSONAPI(&getServiceHandler{db: db})) + http.Handle("/admin/getSession", server.MakeJSONAPI(&getSessionHandler{db: db})) http.Handle("/admin/configureClient", server.MakeJSONAPI(&configureClientHandler{db: db, clients: clients})) http.Handle("/admin/configureService", server.MakeJSONAPI(&configureServiceHandler{db: db, clients: clients})) http.Handle("/admin/configureAuthRealm", server.MakeJSONAPI(&configureAuthRealmHandler{db: db})) diff --git a/src/github.com/matrix-org/go-neb/realms/github/github.go b/src/github.com/matrix-org/go-neb/realms/github/github.go index 7d624e4..848ec1c 100644 --- a/src/github.com/matrix-org/go-neb/realms/github/github.go +++ b/src/github.com/matrix-org/go-neb/realms/github/github.go @@ -30,6 +30,11 @@ type GithubSession struct { realmID string } +// Authenticated returns true if the user has completed the auth process +func (s *GithubSession) Authenticated() bool { + return s.AccessToken != "" +} + // UserID returns the user_id who authorised with Github func (s *GithubSession) UserID() string { return s.userID diff --git a/src/github.com/matrix-org/go-neb/realms/jira/jira.go b/src/github.com/matrix-org/go-neb/realms/jira/jira.go index 26e6fb7..27c5a02 100644 --- a/src/github.com/matrix-org/go-neb/realms/jira/jira.go +++ b/src/github.com/matrix-org/go-neb/realms/jira/jira.go @@ -46,6 +46,11 @@ type JIRASession struct { AccessSecret string } +// Authenticated returns true if the user has completed the auth process +func (s *JIRASession) Authenticated() bool { + return s.AccessToken != "" && s.AccessSecret != "" +} + // UserID returns the ID of the user performing the authentication. func (s *JIRASession) UserID() string { return s.userID diff --git a/src/github.com/matrix-org/go-neb/types/types.go b/src/github.com/matrix-org/go-neb/types/types.go index 14febc5..5e68cfe 100644 --- a/src/github.com/matrix-org/go-neb/types/types.go +++ b/src/github.com/matrix-org/go-neb/types/types.go @@ -123,4 +123,5 @@ type AuthSession interface { ID() string UserID() string RealmID() string + Authenticated() bool }