Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to pass context through a response object to the the st… #83

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions access.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (s *Server) handleAuthorizationCodeRequest(w *Response, r *http.Request) *A

// must be a valid authorization code
var err error
ret.AuthorizeData, err = w.Storage.LoadAuthorize(ret.Code)
ret.AuthorizeData, err = w.Storage.LoadAuthorize(w.Context, ret.Code)
if err != nil {
w.SetError(E_INVALID_GRANT, "")
w.InternalError = err
Expand Down Expand Up @@ -282,7 +282,7 @@ func (s *Server) handleRefreshTokenRequest(w *Response, r *http.Request) *Access

// must be a valid refresh code
var err error
ret.AccessData, err = w.Storage.LoadRefresh(ret.Code)
ret.AccessData, err = w.Storage.LoadRefresh(w.Context, ret.Code)
if err != nil {
w.SetError(E_INVALID_GRANT, "")
w.InternalError = err
Expand Down Expand Up @@ -461,23 +461,23 @@ func (s *Server) FinishAccessRequest(w *Response, r *http.Request, ar *AccessReq
}

// save access token
if err = w.Storage.SaveAccess(ret); err != nil {
if err = w.Storage.SaveAccess(w.Context, ret); err != nil {
w.SetError(E_SERVER_ERROR, "")
w.InternalError = err
return
}

// remove authorization token
if ret.AuthorizeData != nil {
w.Storage.RemoveAuthorize(ret.AuthorizeData.Code)
w.Storage.RemoveAuthorize(w.Context, ret.AuthorizeData.Code)
}

// remove previous access token
if ret.AccessData != nil {
if ret.AccessData.RefreshToken != "" {
w.Storage.RemoveRefresh(ret.AccessData.RefreshToken)
w.Storage.RemoveRefresh(w.Context, ret.AccessData.RefreshToken)
}
w.Storage.RemoveAccess(ret.AccessData.AccessToken)
w.Storage.RemoveAccess(w.Context, ret.AccessData.AccessToken)
}

// output data
Expand All @@ -500,7 +500,7 @@ func (s *Server) FinishAccessRequest(w *Response, r *http.Request, ar *AccessReq
// getClient looks up and authenticates the basic auth using the given
// storage. Sets an error on the response if auth fails or a server error occurs.
func getClient(auth *BasicAuth, storage Storage, w *Response) Client {
client, err := storage.GetClient(auth.Username)
client, err := storage.GetClient(w.Context, auth.Username)
if err != nil {
w.SetError(E_SERVER_ERROR, "")
w.InternalError = err
Expand Down
4 changes: 2 additions & 2 deletions authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s *Server) HandleAuthorizeRequest(w *Response, r *http.Request) *Authorize
}

// must have a valid client
ret.Client, err = w.Storage.GetClient(r.Form.Get("client_id"))
ret.Client, err = w.Storage.GetClient(w.Context, r.Form.Get("client_id"))
if err != nil {
w.SetErrorState(E_SERVER_ERROR, "", ret.State)
w.InternalError = err
Expand Down Expand Up @@ -203,7 +203,7 @@ func (s *Server) FinishAuthorizeRequest(w *Response, r *http.Request, ar *Author
ret.Code = code

// save authorization token
if err = w.Storage.SaveAuthorize(ret); err != nil {
if err = w.Storage.SaveAuthorize(w.Context, ret); err != nil {
w.SetErrorState(E_SERVER_ERROR, "", ar.State)
w.InternalError = err
return
Expand Down
2 changes: 1 addition & 1 deletion info.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *Server) HandleInfoRequest(w *Response, r *http.Request) *InfoRequest {
var err error

// load access data
ret.AccessData, err = w.Storage.LoadAccess(ret.Code)
ret.AccessData, err = w.Storage.LoadAccess(w.Context, ret.Code)
if err != nil {
w.SetError(E_INVALID_REQUEST, "")
w.InternalError = err
Expand Down
1 change: 1 addition & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (

// Server response
type Response struct {
Context interface{}
Type ResponseType
StatusCode int
StatusText string
Expand Down
18 changes: 9 additions & 9 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@ type Storage interface {
Close()

// GetClient loads the client by id (client_id)
GetClient(id string) (Client, error)
GetClient(context interface{}, id string) (Client, error)

// SaveAuthorize saves authorize data.
SaveAuthorize(*AuthorizeData) error
SaveAuthorize(interface{}, *AuthorizeData) error

// LoadAuthorize looks up AuthorizeData by a code.
// Client information MUST be loaded together.
// Optionally can return error if expired.
LoadAuthorize(code string) (*AuthorizeData, error)
LoadAuthorize(context interface{}, code string) (*AuthorizeData, error)

// RemoveAuthorize revokes or deletes the authorization code.
RemoveAuthorize(code string) error
RemoveAuthorize(context interface{}, code string) error

// SaveAccess writes AccessData.
// If RefreshToken is not blank, it must save in a way that can be loaded using LoadRefresh.
SaveAccess(*AccessData) error
SaveAccess(interface{}, *AccessData) error

// LoadAccess retrieves access data by token. Client information MUST be loaded together.
// AuthorizeData and AccessData DON'T NEED to be loaded if not easily available.
// Optionally can return error if expired.
LoadAccess(token string) (*AccessData, error)
LoadAccess(context interface{}, token string) (*AccessData, error)

// RemoveAccess revokes or deletes an AccessData.
RemoveAccess(token string) error
RemoveAccess(context interface{}, token string) error

// LoadRefresh retrieves refresh AccessData. Client information MUST be loaded together.
// AuthorizeData and AccessData DON'T NEED to be loaded if not easily available.
// Optionally can return error if expired.
LoadRefresh(token string) (*AccessData, error)
LoadRefresh(context interface{}, token string) (*AccessData, error)

// RemoveRefresh revokes or deletes refresh AccessData.
RemoveRefresh(token string) error
RemoveRefresh(context interface{}, token string) error
}