Skip to content

Commit b648030

Browse files
Josh WintersRui Yang
authored andcommitted
Add support for client_credentials grant type
Co-authored-by: Rui Yang <[email protected]> Signed-off-by: Josh Winters <[email protected]>
1 parent a28f5bb commit b648030

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

server/handlers.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,8 @@ func (s *Server) handleToken(w http.ResponseWriter, r *http.Request) {
765765
s.handleRefreshToken(w, r, client)
766766
case grantTypePassword:
767767
s.handlePasswordGrant(w, r, client)
768+
case grantTypeClientCredentials:
769+
s.handleClientCredentialsGrant(w, r, client)
768770
default:
769771
s.tokenErrHelper(w, errInvalidGrant, "", http.StatusBadRequest)
770772
}
@@ -1169,6 +1171,29 @@ func (s *Server) handleUserInfo(w http.ResponseWriter, r *http.Request) {
11691171
w.Write(claims)
11701172
}
11711173

1174+
func (s *Server) handleClientCredentialsGrant(w http.ResponseWriter, r *http.Request, client storage.Client) {
1175+
if err := r.ParseForm(); err != nil {
1176+
s.tokenErrHelper(w, errInvalidRequest, "Couldn't parse data", http.StatusBadRequest)
1177+
return
1178+
}
1179+
q := r.Form
1180+
1181+
nonce := q.Get("nonce")
1182+
scopes := strings.Fields(q.Get("scope"))
1183+
1184+
claims := storage.Claims{UserID: client.ID}
1185+
1186+
accessToken := storage.NewID()
1187+
idToken, expiry, err := s.newIDToken(client.ID, claims, scopes, nonce, accessToken, "client")
1188+
if err != nil {
1189+
s.tokenErrHelper(w, errServerError, fmt.Sprintf("failed to create ID token: %v", err), http.StatusInternalServerError)
1190+
return
1191+
}
1192+
1193+
resp := s.toAccessTokenResponse(idToken, accessToken, "", expiry)
1194+
s.writeAccessToken(w, resp)
1195+
}
1196+
11721197
func (s *Server) handlePasswordGrant(w http.ResponseWriter, r *http.Request, client storage.Client) {
11731198
// Parse the fields
11741199
if err := r.ParseForm(); err != nil {

server/oauth2.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ const (
127127
grantTypeRefreshToken = "refresh_token"
128128
grantTypePassword = "password"
129129
grantTypeDeviceCode = "urn:ietf:params:oauth:grant-type:device_code"
130+
grantTypeClientCredentials = "client_credentials"
130131
)
131132

132133
const (

0 commit comments

Comments
 (0)