Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/vendor/github.com/ory/fosite/revoke_handler.go
akuma06 03ea72595d OAuth API [done] (#1275)
* Initial Commit for OAuth API

This builds and run and return the right error.
Need to test it and then adding all users as possible client

* Added mising dependency

* just compile already...

* Fixing template test

* Imrpovements

Moved db stuff in models
Added some tests
Added form in modpanel to add/update a client
Added controllers for add/update of client

* Added Forms + speed improvements

Controller oauth client listing + html
Controller oauth client delete + messages
Messages on comment delete
New ES config that disable ES if set to false. Improve load speed on local development
Fix a load config bug
Fix index admin & translation string sign_out broken by @ewhal

* Sanitize empty strig in form array + css

Multiple empty array of strings are sanitized for the oauth client create form
Added some css for the form display

* Upload and Create form works

* Fix splitting response types

* Removing required on secret when updating

* fix travis error

* Fix travis template test

* Update dependency

* Moved to jinzhu instead of azhao

* randomizen secret on creation

* Final touch on oath api

improved display name
fix grant form csrf
fix login csrf on oauth

* Fix gorm test

* fix template test

* Fixing deleted dependency issue

* Make travis faster

* Fix typo

* Fix csrf for api calls

* This shouldn't be exempt

* Removing hard coded hash

@ewhal Don't forget to replace the hash in tokens.go with another one

* Added an example on how to use OAuth middleware

* Renamed fosite utils to oauth2 utils
2017-07-28 13:46:40 +10:00

106 lignes
3,2 Kio
Go

package fosite
import (
"encoding/json"
"fmt"
"net/http"
"context"
"github.com/pkg/errors"
)
// NewRevocationRequest handles incoming token revocation requests and
// validates various parameters as specified in:
// https://tools.ietf.org/html/rfc7009#section-2.1
//
// The authorization server first validates the client credentials (in
// case of a confidential client) and then verifies whether the token
// was issued to the client making the revocation request. If this
// validation fails, the request is refused and the client is informed
// of the error by the authorization server as described below.
//
// In the next step, the authorization server invalidates the token.
// The invalidation takes place immediately, and the token cannot be
// used again after the revocation.
//
// * https://tools.ietf.org/html/rfc7009#section-2.2
// An invalid token type hint value is ignored by the authorization
// server and does not influence the revocation response.
func (f *Fosite) NewRevocationRequest(ctx context.Context, r *http.Request) error {
if r.Method != "POST" {
return errors.Wrap(ErrInvalidRequest, "HTTP method is not POST")
} else if err := r.ParseForm(); err != nil {
return errors.Wrap(ErrInvalidRequest, err.Error())
}
clientID, clientSecret, ok := r.BasicAuth()
if !ok {
return errors.Wrap(ErrInvalidRequest, "HTTP Authorization header missing or invalid")
}
client, err := f.Store.GetClient(ctx, clientID)
if err != nil {
return errors.Wrap(ErrInvalidClient, err.Error())
}
// Enforce client authentication for confidential clients
if !client.IsPublic() {
if err := f.Hasher.Compare(client.GetHashedSecret(), []byte(clientSecret)); err != nil {
return errors.Wrap(ErrInvalidClient, err.Error())
}
}
token := r.PostForm.Get("token")
tokenTypeHint := TokenType(r.PostForm.Get("token_type_hint"))
var found bool
for _, loader := range f.RevocationHandlers {
if err := loader.RevokeToken(ctx, token, tokenTypeHint); err == nil {
found = true
} else if errors.Cause(err) == ErrUnknownRequest {
// do nothing
} else if err != nil {
return err
}
}
if !found {
return errors.WithStack(ErrInvalidRequest)
}
return nil
}
// WriteRevocationResponse writes a token revocation response as specified in:
// https://tools.ietf.org/html/rfc7009#section-2.2
//
// The authorization server responds with HTTP status code 200 if the
// token has been revoked successfully or if the client submitted an
// invalid token.
//
// Note: invalid tokens do not cause an error response since the client
// cannot handle such an error in a reasonable way. Moreover, the
// purpose of the revocation request, invalidating the particular token,
// is already achieved.
func (f *Fosite) WriteRevocationResponse(rw http.ResponseWriter, err error) {
switch errors.Cause(err) {
case ErrInvalidRequest:
fallthrough
case ErrInvalidClient:
rw.Header().Set("Content-Type", "application/json;charset=UTF-8")
rfcerr := ErrorToRFC6749Error(err)
js, err := json.Marshal(rfcerr)
if err != nil {
http.Error(rw, fmt.Sprintf(`{"error": "%s"}`, err.Error()), http.StatusInternalServerError)
return
}
rw.WriteHeader(rfcerr.Code)
rw.Write(js)
default:
// 200 OK
rw.WriteHeader(http.StatusOK)
}
}