Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions example/server/server.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/go-oauth2/oauth2/v4/generates"
"io"
"log"
"net/http"
Expand All @@ -13,6 +13,8 @@ import (
"os"
"time"

"github.com/go-oauth2/oauth2/v4/generates"

"github.com/go-oauth2/oauth2/v4/errors"
"github.com/go-oauth2/oauth2/v4/manage"
"github.com/go-oauth2/oauth2/v4/models"
Expand Down Expand Up @@ -62,7 +64,7 @@ func main() {

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

srv.SetPasswordAuthorizationHandler(func(username, password string) (userID string, err error) {
srv.SetPasswordAuthorizationHandler(func(ctx context.Context, username, password string) (userID string, err error) {
if username == "test" && password == "test" {
userID = "test"
}
Expand Down Expand Up @@ -143,7 +145,7 @@ func main() {
log.Printf("Server is running at %d port.\n", portvar)
log.Printf("Point your OAuth client Auth endpoint to %s:%d%s", "http://localhost", portvar, "/oauth/authorize")
log.Printf("Point your OAuth client Token endpoint to %s:%d%s", "http://localhost", portvar, "/oauth/token")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d",portvar), nil))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", portvar), nil))
}

func dumpRequest(writer io.Writer, header string, r *http.Request) error {
Expand Down
3 changes: 2 additions & 1 deletion server/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"context"
"net/http"
"time"

Expand All @@ -22,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(username, password string) (userID string, err error)
PasswordAuthorizationHandler func(ctx context.Context, username, password 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(username, password string) (string, error) {
srv.PasswordAuthorizationHandler = func(ctx context.Context, username, password string) (string, error) {
return "", errors.ErrAccessDenied
}
return srv
Expand Down Expand Up @@ -347,7 +347,7 @@ func (s *Server) ValidationTokenRequest(r *http.Request) (oauth2.GrantType, *oau
return "", nil, errors.ErrInvalidRequest
}

userID, err := s.PasswordAuthorizationHandler(username, password)
userID, err := s.PasswordAuthorizationHandler(r.Context(), username, password)
if err != nil {
return "", nil, err
} else if userID == "" {
Expand Down
3 changes: 2 additions & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server_test

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -250,7 +251,7 @@ func TestPasswordCredentials(t *testing.T) {

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