forked from golang/oauth2
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support rfc7523 in client credentials flow
Implement JSON Web Token Profile for OAuth 2.0 Client Authentication in client credentials flow. See https://tools.ietf.org/html/rfc7523 See https://openid.net/specs/openid-connect-core-1_0.html Fixes golang#433
- Loading branch information
1 parent
5d25da1
commit 56f9a0b
Showing
5 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package clientcredentials | ||
|
||
import ( | ||
"math/rand" | ||
"net/url" | ||
"time" | ||
|
||
"golang.org/x/oauth2/internal" | ||
"golang.org/x/oauth2/jws" | ||
) | ||
|
||
const ( | ||
clientAssertionType = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" | ||
) | ||
|
||
var ( | ||
defaultHeader = &jws.Header{Algorithm: "RS256", Typ: "JWT"} | ||
) | ||
|
||
func init() { | ||
rand.Seed(time.Now().UnixNano()) | ||
} | ||
|
||
var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") | ||
|
||
func randJWTID(n int) string { | ||
b := make([]byte, n) | ||
for i := range b { | ||
b[i] = letters[rand.Intn(len(letters))] | ||
} | ||
return string(b) | ||
} | ||
|
||
func (c *tokenSource) jwtAssertionValues() (url.Values, error) { | ||
v := url.Values{ | ||
"grant_type": {"client_credentials"}, | ||
} | ||
pk, err := internal.ParseKey(c.conf.PrivateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
claimSet := &jws.ClaimSet{ | ||
Iss: c.conf.ClientID, | ||
Sub: c.conf.ClientID, | ||
Aud: c.conf.TokenURL, | ||
} | ||
|
||
claimSet.Jti = randJWTID(36) | ||
if t := c.conf.JWTExpires; t > 0 { | ||
claimSet.Exp = time.Now().Add(t).Unix() | ||
} else { | ||
claimSet.Exp = time.Now().Add(time.Hour).Unix() | ||
} | ||
|
||
h := *defaultHeader | ||
h.KeyID = c.conf.KeyID | ||
payload, err := jws.Encode(&h, claimSet, pk) | ||
if err != nil { | ||
return nil, err | ||
} | ||
v.Set("client_assertion", payload) | ||
v.Set("client_assertion_type", clientAssertionType) | ||
|
||
return v, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters