From b3ff1cd54a86f3aad8ffec39067f8a43f83c1032 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 15 Aug 2016 10:10:15 +0100 Subject: [PATCH 1/2] Add endpoint for GETing AuthSession information for a user/realm tuple Add a new `AuthSession` function `Authenticated()` which returns `true` if the user has completed the auth process. This allows the caller to distinguish between: - Never done any auth (404s) - In the process of doing auth (`Authenticated == false`) - Finished doing auth (`Authenticated == true`) --- src/github.com/matrix-org/go-neb/api.go | 35 +++++++++++++++++++ src/github.com/matrix-org/go-neb/goneb.go | 1 + .../matrix-org/go-neb/realms/github/github.go | 5 +++ .../matrix-org/go-neb/realms/jira/jira.go | 5 +++ .../matrix-org/go-neb/types/types.go | 1 + 5 files changed, 47 insertions(+) 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 } From cb0fcbf2addc14fe4bc4af449a4d7043bac1aaef Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 15 Aug 2016 10:47:16 +0100 Subject: [PATCH 2/2] s/GET/POST/ --- src/github.com/matrix-org/go-neb/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/api.go b/src/github.com/matrix-org/go-neb/api.go index 6d63678..598a35b 100644 --- a/src/github.com/matrix-org/go-neb/api.go +++ b/src/github.com/matrix-org/go-neb/api.go @@ -269,7 +269,7 @@ type getSessionHandler struct { } func (h *getSessionHandler) OnIncomingRequest(req *http.Request) (interface{}, *errors.HTTPError) { - if req.Method != "GET" { + if req.Method != "POST" { return nil, &errors.HTTPError{nil, "Unsupported Method", 405} } var body struct {