* 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
172 lignes
5,7 Kio
Go
172 lignes
5,7 Kio
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/NyaaPantsu/nyaa/models"
|
|
"github.com/NyaaPantsu/nyaa/models/oauth_server"
|
|
"github.com/NyaaPantsu/nyaa/models/users"
|
|
"github.com/NyaaPantsu/nyaa/utils/log"
|
|
"github.com/ory/fosite"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type FositeSQLStore struct {
|
|
models.Manager
|
|
}
|
|
|
|
func sqlSchemaFromRequest(signature string, r fosite.Requester) (*models.OauthAbstract, error) {
|
|
if r.GetSession() == nil {
|
|
log.Debugf("Got an empty session in sqlSchemaFromRequest")
|
|
}
|
|
|
|
session, err := json.Marshal(r.GetSession())
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return &models.OauthAbstract{
|
|
RequestID: r.GetID(),
|
|
Signature: signature,
|
|
RequestedAt: r.GetRequestedAt(),
|
|
ClientID: r.GetClient().GetID(),
|
|
Scope: strings.Join([]string(r.GetRequestedScopes()), "|"),
|
|
GrantedScope: strings.Join([]string(r.GetGrantedScopes()), "|"),
|
|
FormData: r.GetRequestForm().Encode(),
|
|
SessionData: session,
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (s *FositeSQLStore) createSession(signature string, requester fosite.Requester, table string) error {
|
|
data, err := sqlSchemaFromRequest(signature, requester)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
model := oauth_server.SelectModel(table, *data)
|
|
err = models.ORM.Create(model).Error
|
|
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *FositeSQLStore) findSessionBySignature(signature string, session fosite.Session, table string) (fosite.Requester, error) {
|
|
d, err := oauth_server.FindBySignature(signature, table)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return d.ToRequest(session, s.Manager)
|
|
}
|
|
|
|
func (s *FositeSQLStore) deleteSession(signature string, table string) error {
|
|
return oauth_server.DeleteBySession(signature, table)
|
|
}
|
|
|
|
func (s *FositeSQLStore) CreateOpenIDConnectSession(_ context.Context, signature string, requester fosite.Requester) error {
|
|
return s.createSession(signature, requester, models.TableOpenID)
|
|
}
|
|
|
|
func (s *FositeSQLStore) GetOpenIDConnectSession(_ context.Context, signature string, requester fosite.Requester) (fosite.Requester, error) {
|
|
return s.findSessionBySignature(signature, requester.GetSession(), models.TableOpenID)
|
|
}
|
|
|
|
func (s *FositeSQLStore) DeleteOpenIDConnectSession(_ context.Context, signature string) error {
|
|
return s.deleteSession(signature, models.TableOpenID)
|
|
}
|
|
|
|
func (s *FositeSQLStore) CreateAuthorizeCodeSession(_ context.Context, signature string, requester fosite.Requester) error {
|
|
return s.createSession(signature, requester, models.TableCode)
|
|
}
|
|
|
|
func (s *FositeSQLStore) GetAuthorizeCodeSession(_ context.Context, signature string, session fosite.Session) (fosite.Requester, error) {
|
|
return s.findSessionBySignature(signature, session, models.TableCode)
|
|
}
|
|
|
|
func (s *FositeSQLStore) DeleteAuthorizeCodeSession(_ context.Context, signature string) error {
|
|
return s.deleteSession(signature, models.TableCode)
|
|
}
|
|
|
|
func (s *FositeSQLStore) CreateAccessTokenSession(_ context.Context, signature string, requester fosite.Requester) error {
|
|
return s.createSession(signature, requester, models.TableAccess)
|
|
}
|
|
|
|
func (s *FositeSQLStore) GetAccessTokenSession(_ context.Context, signature string, session fosite.Session) (fosite.Requester, error) {
|
|
return s.findSessionBySignature(signature, session, models.TableAccess)
|
|
}
|
|
|
|
func (s *FositeSQLStore) DeleteAccessTokenSession(_ context.Context, signature string) error {
|
|
return s.deleteSession(signature, models.TableAccess)
|
|
}
|
|
|
|
func (s *FositeSQLStore) CreateRefreshTokenSession(_ context.Context, signature string, requester fosite.Requester) error {
|
|
return s.createSession(signature, requester, models.TableRefresh)
|
|
}
|
|
|
|
func (s *FositeSQLStore) GetRefreshTokenSession(_ context.Context, signature string, session fosite.Session) (fosite.Requester, error) {
|
|
return s.findSessionBySignature(signature, session, models.TableRefresh)
|
|
}
|
|
|
|
func (s *FositeSQLStore) DeleteRefreshTokenSession(_ context.Context, signature string) error {
|
|
return s.deleteSession(signature, models.TableRefresh)
|
|
}
|
|
|
|
func (s *FositeSQLStore) CreateImplicitAccessTokenSession(ctx context.Context, signature string, requester fosite.Requester) error {
|
|
return s.CreateAccessTokenSession(ctx, signature, requester)
|
|
}
|
|
|
|
func (s *FositeSQLStore) PersistAuthorizeCodeGrantSession(ctx context.Context, authorizeCode, accessSignature, refreshSignature string, request fosite.Requester) error {
|
|
if err := s.DeleteAuthorizeCodeSession(ctx, authorizeCode); err != nil {
|
|
return err
|
|
} else if err := s.CreateAccessTokenSession(ctx, accessSignature, request); err != nil {
|
|
return err
|
|
}
|
|
|
|
if refreshSignature == "" {
|
|
return nil
|
|
}
|
|
|
|
if err := s.CreateRefreshTokenSession(ctx, refreshSignature, request); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *FositeSQLStore) PersistRefreshTokenGrantSession(ctx context.Context, originalRefreshSignature, accessSignature, refreshSignature string, request fosite.Requester) error {
|
|
if err := s.DeleteRefreshTokenSession(ctx, originalRefreshSignature); err != nil {
|
|
return err
|
|
} else if err := s.CreateAccessTokenSession(ctx, accessSignature, request); err != nil {
|
|
return err
|
|
} else if err := s.CreateRefreshTokenSession(ctx, refreshSignature, request); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *FositeSQLStore) RevokeRefreshToken(ctx context.Context, id string) error {
|
|
return s.revokeSession(id, models.TableRefresh)
|
|
}
|
|
|
|
func (s *FositeSQLStore) RevokeAccessToken(ctx context.Context, id string) error {
|
|
return s.revokeSession(id, models.TableAccess)
|
|
}
|
|
|
|
func (s *FositeSQLStore) revokeSession(id string, table string) error {
|
|
return oauth_server.DeleteByID(id, table)
|
|
}
|
|
|
|
func (s *FositeSQLStore) Authenticate(_ context.Context, name string, secret string) error {
|
|
_, _, err := users.Exists(name, secret)
|
|
if err != nil {
|
|
return fosite.ErrNotFound
|
|
}
|
|
return nil
|
|
}
|