Skip to content

Commit

Permalink
add public field to Client and ClientInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Jan 20, 2023
1 parent f108728 commit e0f39b9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
8 changes: 2 additions & 6 deletions manage/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,8 @@ func (m *Manager) GenerateAccessToken(ctx context.Context, gt oauth2.GrantType,
if !cliPass.VerifyPassword(tgr.ClientSecret) {
return nil, errors.ErrInvalidClient
}
} else if len(cli.GetSecret()) > 0 && tgr.ClientSecret != cli.GetSecret() {
// auth code flow doesnt require client_secret if used with PKCE and state parameter
// this is especially useful for mobile apps, that cant hold the secret
if !(gt == oauth2.AuthorizationCode && tgr.ClientSecret == "" && tgr.CodeVerifier != "") {
return nil, errors.ErrInvalidClient
}
} else if cli.IsPublic() == false && len(cli.GetSecret()) > 0 && tgr.ClientSecret != cli.GetSecret() {
return nil, errors.ErrInvalidClient
}
if tgr.RedirectURI != "" {
if err := m.validateURI(cli.GetDomain(), tgr.RedirectURI); err != nil {
Expand Down
1 change: 1 addition & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type (
GetID() string
GetSecret() string
GetDomain() string
IsPublic() bool
GetUserID() string
}

Expand Down
6 changes: 6 additions & 0 deletions models/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Client struct {
ID string
Secret string
Domain string
Public bool
UserID string
}

Expand All @@ -23,6 +24,11 @@ func (c *Client) GetDomain() string {
return c.Domain
}

// GetUserID user id
func (c *Client) IsPublic() bool {
return c.Public
}

// GetUserID user id
func (c *Client) GetUserID() string {
return c.UserID
Expand Down
17 changes: 9 additions & 8 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ func init() {
manager.MustTokenStorage(store.NewMemoryTokenStore())
}

func clientStore(domain string) oauth2.ClientStore {
func clientStore(domain string, public bool) oauth2.ClientStore {
clientStore := store.NewClientStore()
clientStore.Set(clientID, &models.Client{
ID: clientID,
Secret: clientSecret,
Domain: domain,
Public: public,
})
return clientStore
}
Expand Down Expand Up @@ -95,7 +96,7 @@ func TestAuthorizeCode(t *testing.T) {
}))
defer csrv.Close()

manager.MapClientStorage(clientStore(csrv.URL))
manager.MapClientStorage(clientStore(csrv.URL, true))
srv = server.NewDefaultServer(manager)
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
userID = "000000"
Expand Down Expand Up @@ -146,7 +147,7 @@ func TestAuthorizeCodeWithChallengePlain(t *testing.T) {
}))
defer csrv.Close()

manager.MapClientStorage(clientStore(csrv.URL))
manager.MapClientStorage(clientStore(csrv.URL, true))
srv = server.NewDefaultServer(manager)
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
userID = "000000"
Expand Down Expand Up @@ -199,7 +200,7 @@ func TestAuthorizeCodeWithChallengeS256(t *testing.T) {
}))
defer csrv.Close()

manager.MapClientStorage(clientStore(csrv.URL))
manager.MapClientStorage(clientStore(csrv.URL, true))
srv = server.NewDefaultServer(manager)
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
userID = "000000"
Expand Down Expand Up @@ -228,7 +229,7 @@ func TestImplicit(t *testing.T) {
csrv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer csrv.Close()

manager.MapClientStorage(clientStore(csrv.URL))
manager.MapClientStorage(clientStore(csrv.URL, false))
srv = server.NewDefaultServer(manager)
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
userID = "000000"
Expand All @@ -251,7 +252,7 @@ func TestPasswordCredentials(t *testing.T) {
defer tsrv.Close()
e := httpexpect.New(t, tsrv.URL)

manager.MapClientStorage(clientStore(""))
manager.MapClientStorage(clientStore("", false))
srv = server.NewDefaultServer(manager)
srv.SetPasswordAuthorizationHandler(func(ctx context.Context, clientID, username, password string) (userID string, err error) {
if username == "admin" && password == "123456" {
Expand Down Expand Up @@ -284,7 +285,7 @@ func TestClientCredentials(t *testing.T) {
defer tsrv.Close()
e := httpexpect.New(t, tsrv.URL)

manager.MapClientStorage(clientStore(""))
manager.MapClientStorage(clientStore("", false))

srv = server.NewDefaultServer(manager)
srv.SetClientInfoHandler(server.ClientFormHandler)
Expand Down Expand Up @@ -374,7 +375,7 @@ func TestRefreshing(t *testing.T) {
}))
defer csrv.Close()

manager.MapClientStorage(clientStore(csrv.URL))
manager.MapClientStorage(clientStore(csrv.URL, true))
srv = server.NewDefaultServer(manager)
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
userID = "000000"
Expand Down

0 comments on commit e0f39b9

Please sign in to comment.