Skip to content

Commit

Permalink
Add per client authorization if needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
aurelien-semence committed May 2, 2022
1 parent 446d602 commit c9f79d9
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion example/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func main() {

srv := server.NewServer(server.NewConfig(), manager)

srv.SetPasswordAuthorizationHandler(func(ctx context.Context, username, password string) (userID string, err error) {
srv.SetPasswordAuthorizationHandler(func(ctx context.Context, username, password, clientID string) (userID string, err error) {
if username == "test" && password == "test" {
userID = "test"
}
Expand Down
2 changes: 1 addition & 1 deletion server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type (
UserAuthorizationHandler func(w http.ResponseWriter, r *http.Request) (userID string, err error)

// PasswordAuthorizationHandler get user id from username and password
PasswordAuthorizationHandler func(ctx context.Context, username, password string) (userID string, err error)
PasswordAuthorizationHandler func(ctx context.Context, username, password, clientID string) (userID string, err error)

// RefreshingScopeHandler check the scope of the refreshing token
RefreshingScopeHandler func(tgr *oauth2.TokenGenerateRequest, oldScope string) (allowed bool, err error)
Expand Down
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewServer(cfg *Config, manager oauth2.Manager) *Server {
return "", errors.ErrAccessDenied
}

srv.PasswordAuthorizationHandler = func(ctx context.Context, username, password string) (string, error) {
srv.PasswordAuthorizationHandler = func(ctx context.Context, clientID, username, password string) (string, error) {
return "", errors.ErrAccessDenied
}
return srv
Expand Down Expand Up @@ -357,7 +357,7 @@ func (s *Server) ValidationTokenRequest(r *http.Request) (oauth2.GrantType, *oau
return "", nil, errors.ErrInvalidRequest
}

userID, err := s.PasswordAuthorizationHandler(r.Context(), username, password)
userID, err := s.PasswordAuthorizationHandler(r.Context(), username, password, clientID)
if err != nil {
return "", nil, err
} else if userID == "" {
Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestPasswordCredentials(t *testing.T) {

manager.MapClientStorage(clientStore(""))
srv = server.NewDefaultServer(manager)
srv.SetPasswordAuthorizationHandler(func(ctx context.Context, username, password string) (userID string, err error) {
srv.SetPasswordAuthorizationHandler(func(ctx context.Context, username, password, clientID string) (userID string, err error) {
if username == "admin" && password == "123456" {
userID = "000000"
return
Expand Down

0 comments on commit c9f79d9

Please sign in to comment.