diff options
author | 2022-06-11 10:39:39 +0200 | |
---|---|---|
committer | 2022-06-11 10:39:39 +0200 | |
commit | 694a49058951de31cca4ea061e2c08d44e712612 (patch) | |
tree | 8509cb95f41faaf15d4352707617bff17300978d /internal/oauth | |
parent | [bugfix] Make accounts media_only query also work with pg (#643) (diff) | |
download | gotosocial-694a49058951de31cca4ea061e2c08d44e712612.tar.xz |
[feature] Add `created_at` and `error_description` to `/oauth/token` endpoint (#645)
* start fiddling about with oauth server
* start returning more helpful errors from oauth
* test helpful(ish) token errors
* add missing license header
Diffstat (limited to 'internal/oauth')
-rw-r--r-- | internal/oauth/errors.go | 26 | ||||
-rw-r--r-- | internal/oauth/server.go | 24 |
2 files changed, 47 insertions, 3 deletions
diff --git a/internal/oauth/errors.go b/internal/oauth/errors.go new file mode 100644 index 000000000..25278bdc9 --- /dev/null +++ b/internal/oauth/errors.go @@ -0,0 +1,26 @@ +/* + GoToSocial + Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package oauth + +import "github.com/superseriousbusiness/oauth2/v4/errors" + +// InvalidRequest returns an oauth spec compliant 'invalid_request' error. +func InvalidRequest() error { + return errors.New("invalid_request") +} diff --git a/internal/oauth/server.go b/internal/oauth/server.go index bfe615832..4dcc41ceb 100644 --- a/internal/oauth/server.go +++ b/internal/oauth/server.go @@ -25,6 +25,7 @@ import ( "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/oauth2/v4" "github.com/superseriousbusiness/oauth2/v4/errors" "github.com/superseriousbusiness/oauth2/v4/manage" @@ -52,7 +53,7 @@ const ( // Server wraps some oauth2 server functions in an interface, exposing only what is needed type Server interface { - HandleTokenRequest(w http.ResponseWriter, r *http.Request) error + HandleTokenRequest(r *http.Request) (map[string]interface{}, gtserror.WithCode) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request) error ValidationBearerToken(r *http.Request) (oauth2.TokenInfo, error) GenerateUserAccessToken(ctx context.Context, ti oauth2.TokenInfo, clientSecret string, userID string) (accessToken oauth2.TokenInfo, err error) @@ -116,8 +117,25 @@ func New(ctx context.Context, database db.Basic) Server { } // HandleTokenRequest wraps the oauth2 library's HandleTokenRequest function -func (s *s) HandleTokenRequest(w http.ResponseWriter, r *http.Request) error { - return s.server.HandleTokenRequest(w, r) +func (s *s) HandleTokenRequest(r *http.Request) (map[string]interface{}, gtserror.WithCode) { + ctx := r.Context() + + gt, tgr, err := s.server.ValidationTokenRequest(r) + if err != nil { + help := fmt.Sprintf("could not validate token request: %s", err) + return nil, gtserror.NewErrorBadRequest(err, help) + } + + ti, err := s.server.GetAccessToken(ctx, gt, tgr) + if err != nil { + help := fmt.Sprintf("could not get access token: %s", err) + return nil, gtserror.NewErrorBadRequest(err, help) + } + + data := s.server.GetTokenData(ti) + data["created_at"] = ti.GetAccessCreateAt().Unix() + + return data, nil } // HandleAuthorizeRequest wraps the oauth2 library's HandleAuthorizeRequest function |