Skip to content

Commit 723f5fc

Browse files
author
lleadbet
committed
adding body param support for client credentials
1 parent de5d1ff commit 723f5fc

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

internal/mock_auth/app_access_token.go

+39-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515

1616
type AppAccessTokenEndpoint struct{}
1717

18+
type AppAccessTokenRequestBody struct {
19+
ClientID string `json:"client_id"`
20+
ClientSecret string `json:"client_secret"`
21+
GrantType string `json:"grant_type"`
22+
Scope string `json:"scope"`
23+
}
24+
1825
type AppAccessTokenEndpointResposne struct {
1926
AccessToken string `json:"access_token"`
2027
RefreshToken string `json:"refresh_token"`
@@ -33,12 +40,37 @@ func (e AppAccessTokenEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request
3340
return
3441
}
3542

36-
clientID := r.URL.Query().Get("client_id")
37-
clientSecret := r.URL.Query().Get("client_secret")
38-
grantType := r.URL.Query().Get("grant_type")
39-
scope := r.URL.Query().Get("scope")
40-
scopes := strings.Split(scope, " ")
41-
if clientID == "" || clientSecret == "" || grantType != "client_credentials" {
43+
params := AppAccessTokenRequestBody{
44+
ClientID: r.URL.Query().Get("client_id"),
45+
ClientSecret: r.URL.Query().Get("client_secret"),
46+
GrantType: r.URL.Query().Get("grant_type"),
47+
Scope: r.URL.Query().Get("scope"),
48+
}
49+
50+
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
51+
err := r.ParseForm()
52+
if err != nil {
53+
mock_errors.WriteServerError(w, err.Error())
54+
return
55+
}
56+
57+
if r.Form.Get("client_id") != "" {
58+
params.ClientID = r.Form.Get("client_id")
59+
}
60+
if r.Form.Get("client_secret") != "" {
61+
params.ClientSecret = r.Form.Get("client_secret")
62+
}
63+
if r.Form.Get("grant_type") != "" {
64+
params.GrantType = r.Form.Get("grant_type")
65+
}
66+
if r.Form.Get("scope") != "" {
67+
params.Scope = r.Form.Get("scope")
68+
}
69+
}
70+
71+
scopes := strings.Split(params.Scope, " ")
72+
73+
if params.ClientID == "" || params.ClientSecret == "" || params.GrantType != "client_credentials" {
4274
w.WriteHeader(http.StatusBadRequest)
4375
return
4476
}
@@ -55,7 +87,7 @@ func (e AppAccessTokenEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request
5587
return
5688
}
5789

58-
res, err := db.NewQuery(r, 10).GetAuthenticationClient(database.AuthenticationClient{ID: clientID, Secret: clientSecret})
90+
res, err := db.NewQuery(r, 10).GetAuthenticationClient(database.AuthenticationClient{ID: params.ClientID, Secret: params.ClientSecret})
5991
if err != nil {
6092
mock_errors.WriteServerError(w, err.Error())
6193
return

0 commit comments

Comments
 (0)