Skip to content

Commit 79d5489

Browse files
authored
Merge pull request #207 from realbucksavage/feat/context-in-password-handler
Added context to PasswordAuthroizationHandler (#173)
2 parents ea58e54 + 8998205 commit 79d5489

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

example/server/server.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"flag"
67
"fmt"
7-
"github.com/go-oauth2/oauth2/v4/generates"
88
"io"
99
"log"
1010
"net/http"
@@ -13,6 +13,8 @@ import (
1313
"os"
1414
"time"
1515

16+
"github.com/go-oauth2/oauth2/v4/generates"
17+
1618
"github.com/go-oauth2/oauth2/v4/errors"
1719
"github.com/go-oauth2/oauth2/v4/manage"
1820
"github.com/go-oauth2/oauth2/v4/models"
@@ -62,7 +64,7 @@ func main() {
6264

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

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

149151
func dumpRequest(writer io.Writer, header string, r *http.Request) error {

server/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package server
22

33
import (
4+
"context"
45
"net/http"
56
"time"
67

@@ -22,7 +23,7 @@ type (
2223
UserAuthorizationHandler func(w http.ResponseWriter, r *http.Request) (userID string, err error)
2324

2425
// PasswordAuthorizationHandler get user id from username and password
25-
PasswordAuthorizationHandler func(username, password string) (userID string, err error)
26+
PasswordAuthorizationHandler func(ctx context.Context, username, password string) (userID string, err error)
2627

2728
// RefreshingScopeHandler check the scope of the refreshing token
2829
RefreshingScopeHandler func(tgr *oauth2.TokenGenerateRequest, oldScope string) (allowed bool, err error)

server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewServer(cfg *Config, manager oauth2.Manager) *Server {
3232
return "", errors.ErrAccessDenied
3333
}
3434

35-
srv.PasswordAuthorizationHandler = func(username, password string) (string, error) {
35+
srv.PasswordAuthorizationHandler = func(ctx context.Context, username, password string) (string, error) {
3636
return "", errors.ErrAccessDenied
3737
}
3838
return srv
@@ -347,7 +347,7 @@ func (s *Server) ValidationTokenRequest(r *http.Request) (oauth2.GrantType, *oau
347347
return "", nil, errors.ErrInvalidRequest
348348
}
349349

350-
userID, err := s.PasswordAuthorizationHandler(username, password)
350+
userID, err := s.PasswordAuthorizationHandler(r.Context(), username, password)
351351
if err != nil {
352352
return "", nil, err
353353
} else if userID == "" {

server/server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package server_test
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"net/http/httptest"
@@ -250,7 +251,7 @@ func TestPasswordCredentials(t *testing.T) {
250251

251252
manager.MapClientStorage(clientStore(""))
252253
srv = server.NewDefaultServer(manager)
253-
srv.SetPasswordAuthorizationHandler(func(username, password string) (userID string, err error) {
254+
srv.SetPasswordAuthorizationHandler(func(ctx context.Context, username, password string) (userID string, err error) {
254255
if username == "admin" && password == "123456" {
255256
userID = "000000"
256257
return

0 commit comments

Comments
 (0)