Skip to content

Commit 8f0d487

Browse files
authored
Merge pull request #234 from MaindeckAS/supportPublicGrantsWithIsPublicFunction
Introducing public field to client models
2 parents 62bc01d + 4d9fa1e commit 8f0d487

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

manage/manager.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (m *Manager) GenerateAuthToken(ctx context.Context, rt oauth2.ResponseType,
218218
}
219219
return ti, nil
220220
}
221-
221+
222222
// get authorization code data
223223
func (m *Manager) getAuthorizationCode(ctx context.Context, code string) (oauth2.TokenInfo, error) {
224224
ti, err := m.tokenStore.GetByCode(ctx, code)
@@ -296,6 +296,10 @@ func (m *Manager) GenerateAccessToken(ctx context.Context, gt oauth2.GrantType,
296296
}
297297
}
298298

299+
if gt == oauth2.ClientCredentials && cli.IsPublic() == true {
300+
return nil, errors.ErrInvalidClient
301+
}
302+
299303
if gt == oauth2.AuthorizationCode {
300304
ti, err := m.getAndDelAuthorizationCode(ctx, tgr)
301305
if err != nil {

model.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type (
1010
GetID() string
1111
GetSecret() string
1212
GetDomain() string
13+
IsPublic() bool
1314
GetUserID() string
1415
}
1516

models/client.go

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type Client struct {
55
ID string
66
Secret string
77
Domain string
8+
Public bool
89
UserID string
910
}
1011

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

27+
// IsPublic public
28+
func (c *Client) IsPublic() bool {
29+
return c.Public
30+
}
31+
2632
// GetUserID user id
2733
func (c *Client) GetUserID() string {
2834
return c.UserID

server/server_test.go

+29-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8-
"net/url"
98
"testing"
109

1110
"github.com/gavv/httpexpect"
@@ -26,22 +25,30 @@ var (
2625
clientSecret = "11111111"
2726

2827
plainChallenge = "ThisIsAFourtyThreeCharactersLongStringThing"
29-
s256Challenge = "s256test"
30-
// echo s256test | sha256 | base64 | tr '/+' '_-'
31-
s256ChallengeHash = "W6YWc_4yHwYN-cGDgGmOMHF3l7KDy7VcRjf7q2FVF-o="
28+
s256Challenge = "s256tests256tests256tests256tests256tests256test"
29+
// sha2562 := sha256.Sum256([]byte(s256Challenge))
30+
// fmt.Printf(base64.URLEncoding.EncodeToString(sha2562[:]))
31+
s256ChallengeHash = "To2Xqv01cm16bC9Sf7KRRS8CO2SFss_HSMQOr3sdCDE="
3232
)
3333

3434
func init() {
3535
manager = manage.NewDefaultManager()
3636
manager.MustTokenStorage(store.NewMemoryTokenStore())
3737
}
3838

39-
func clientStore(domain string) oauth2.ClientStore {
39+
func clientStore(domain string, public bool) oauth2.ClientStore {
4040
clientStore := store.NewClientStore()
41+
var secret string
42+
if public {
43+
secret = ""
44+
} else {
45+
secret = clientSecret
46+
}
4147
clientStore.Set(clientID, &models.Client{
4248
ID: clientID,
43-
Secret: clientSecret,
49+
Secret: secret,
4450
Domain: domain,
51+
Public: public,
4552
})
4653
return clientStore
4754
}
@@ -95,7 +102,7 @@ func TestAuthorizeCode(t *testing.T) {
95102
}))
96103
defer csrv.Close()
97104

98-
manager.MapClientStorage(clientStore(csrv.URL))
105+
manager.MapClientStorage(clientStore(csrv.URL, true))
99106
srv = server.NewDefaultServer(manager)
100107
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
101108
userID = "000000"
@@ -107,7 +114,7 @@ func TestAuthorizeCode(t *testing.T) {
107114
WithQuery("client_id", clientID).
108115
WithQuery("scope", "all").
109116
WithQuery("state", "123").
110-
WithQuery("redirect_uri", url.QueryEscape(csrv.URL+"/oauth2")).
117+
WithQuery("redirect_uri", csrv.URL+"/oauth2").
111118
Expect().Status(http.StatusOK)
112119
}
113120

@@ -134,7 +141,7 @@ func TestAuthorizeCodeWithChallengePlain(t *testing.T) {
134141
WithFormField("grant_type", "authorization_code").
135142
WithFormField("client_id", clientID).
136143
WithFormField("code", code).
137-
WithBasicAuth("code_verifier", "testchallenge").
144+
WithFormField("code_verifier", plainChallenge).
138145
Expect().
139146
Status(http.StatusOK).
140147
JSON().Object()
@@ -146,19 +153,20 @@ func TestAuthorizeCodeWithChallengePlain(t *testing.T) {
146153
}))
147154
defer csrv.Close()
148155

149-
manager.MapClientStorage(clientStore(csrv.URL))
156+
manager.MapClientStorage(clientStore(csrv.URL, true))
150157
srv = server.NewDefaultServer(manager)
151158
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
152159
userID = "000000"
153160
return
154161
})
162+
srv.SetClientInfoHandler(server.ClientFormHandler)
155163

156164
e.GET("/authorize").
157165
WithQuery("response_type", "code").
158166
WithQuery("client_id", clientID).
159167
WithQuery("scope", "all").
160168
WithQuery("state", "123").
161-
WithQuery("redirect_uri", url.QueryEscape(csrv.URL+"/oauth2")).
169+
WithQuery("redirect_uri", csrv.URL+"/oauth2").
162170
WithQuery("code_challenge", plainChallenge).
163171
Expect().Status(http.StatusOK)
164172
}
@@ -186,7 +194,7 @@ func TestAuthorizeCodeWithChallengeS256(t *testing.T) {
186194
WithFormField("grant_type", "authorization_code").
187195
WithFormField("client_id", clientID).
188196
WithFormField("code", code).
189-
WithBasicAuth("code_verifier", s256Challenge).
197+
WithFormField("code_verifier", s256Challenge).
190198
Expect().
191199
Status(http.StatusOK).
192200
JSON().Object()
@@ -198,19 +206,20 @@ func TestAuthorizeCodeWithChallengeS256(t *testing.T) {
198206
}))
199207
defer csrv.Close()
200208

201-
manager.MapClientStorage(clientStore(csrv.URL))
209+
manager.MapClientStorage(clientStore(csrv.URL, true))
202210
srv = server.NewDefaultServer(manager)
203211
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
204212
userID = "000000"
205213
return
206214
})
215+
srv.SetClientInfoHandler(server.ClientFormHandler)
207216

208217
e.GET("/authorize").
209218
WithQuery("response_type", "code").
210219
WithQuery("client_id", clientID).
211220
WithQuery("scope", "all").
212221
WithQuery("state", "123").
213-
WithQuery("redirect_uri", url.QueryEscape(csrv.URL+"/oauth2")).
222+
WithQuery("redirect_uri", csrv.URL+"/oauth2").
214223
WithQuery("code_challenge", s256ChallengeHash).
215224
WithQuery("code_challenge_method", "S256").
216225
Expect().Status(http.StatusOK)
@@ -226,7 +235,7 @@ func TestImplicit(t *testing.T) {
226235
csrv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
227236
defer csrv.Close()
228237

229-
manager.MapClientStorage(clientStore(csrv.URL))
238+
manager.MapClientStorage(clientStore(csrv.URL, false))
230239
srv = server.NewDefaultServer(manager)
231240
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
232241
userID = "000000"
@@ -238,7 +247,7 @@ func TestImplicit(t *testing.T) {
238247
WithQuery("client_id", clientID).
239248
WithQuery("scope", "all").
240249
WithQuery("state", "123").
241-
WithQuery("redirect_uri", url.QueryEscape(csrv.URL+"/oauth2")).
250+
WithQuery("redirect_uri", csrv.URL+"/oauth2").
242251
Expect().Status(http.StatusOK)
243252
}
244253

@@ -249,7 +258,7 @@ func TestPasswordCredentials(t *testing.T) {
249258
defer tsrv.Close()
250259
e := httpexpect.New(t, tsrv.URL)
251260

252-
manager.MapClientStorage(clientStore(""))
261+
manager.MapClientStorage(clientStore("", false))
253262
srv = server.NewDefaultServer(manager)
254263
srv.SetPasswordAuthorizationHandler(func(ctx context.Context, clientID, username, password string) (userID string, err error) {
255264
if username == "admin" && password == "123456" {
@@ -282,7 +291,7 @@ func TestClientCredentials(t *testing.T) {
282291
defer tsrv.Close()
283292
e := httpexpect.New(t, tsrv.URL)
284293

285-
manager.MapClientStorage(clientStore(""))
294+
manager.MapClientStorage(clientStore("", false))
286295

287296
srv = server.NewDefaultServer(manager)
288297
srv.SetClientInfoHandler(server.ClientFormHandler)
@@ -372,7 +381,7 @@ func TestRefreshing(t *testing.T) {
372381
}))
373382
defer csrv.Close()
374383

375-
manager.MapClientStorage(clientStore(csrv.URL))
384+
manager.MapClientStorage(clientStore(csrv.URL, true))
376385
srv = server.NewDefaultServer(manager)
377386
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
378387
userID = "000000"
@@ -384,7 +393,7 @@ func TestRefreshing(t *testing.T) {
384393
WithQuery("client_id", clientID).
385394
WithQuery("scope", "all").
386395
WithQuery("state", "123").
387-
WithQuery("redirect_uri", url.QueryEscape(csrv.URL+"/oauth2")).
396+
WithQuery("redirect_uri", csrv.URL+"/oauth2").
388397
Expect().Status(http.StatusOK)
389398
}
390399

0 commit comments

Comments
 (0)