Skip to content

Commit 87f819f

Browse files
committed
Add client code and test for JWT assertion support
1 parent cd49cac commit 87f819f

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

client_agent.go

+13
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ func (c ClientAgent) RefreshToken(refreshToken, scope string) (*Authorization, e
9393
})
9494
}
9595

96+
// Assertion implements the assertion grant type as defined by https://tools.ietf.org/html/rfc7521
97+
func (c ClientAgent) Assertion(profile, assertion, scope string) (*Authorization, error) {
98+
return c.doTokenRequest(url.Values{
99+
"grant_type": []string{profile},
100+
"assertion": []string{assertion},
101+
"scope": []string{scope},
102+
})
103+
}
104+
105+
var (
106+
AssertionJWT = "urn:ietf:params:oauth:grant-type:jwt-bearer"
107+
)
108+
96109
// doTokenRequest performs a request against token endpoint and returns a Authorization.
97110
func (c ClientAgent) doTokenRequest(params url.Values) (*Authorization, error) {
98111
body := []byte(params.Encode())

integration_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
"net/url"
2525
"reflect"
2626
"testing"
27+
"time"
2728

29+
"github.com/gostack/jwt"
2830
"github.com/gostack/oauth2"
2931
)
3032

@@ -245,6 +247,41 @@ func TestPasswordGrantType(t *testing.T) {
245247
}
246248
}
247249

250+
func TestAssertionGrantType(t *testing.T) {
251+
p, clt, srv := setupProvider()
252+
defer srv.Close()
253+
254+
tk := jwt.NewToken()
255+
tk.JWTID = "id"
256+
tk.Issuer = "http://client.jwt.test"
257+
tk.Subject = "[email protected]"
258+
tk.Audience = "http://authz.jwt.test"
259+
tk.Expires = time.Now().Add(time.Minute * 1)
260+
261+
signedTk, err := tk.Sign(clt.Secret)
262+
if err != nil {
263+
t.Fatal(err)
264+
}
265+
266+
a, err := clt.Assertion(oauth2.AssertionJWT, signedTk, "basic_profile email")
267+
if err != nil {
268+
t.Fatal(err)
269+
}
270+
271+
a2, err := p.GetAuthorizationByAccessToken(a.AccessToken)
272+
if err != nil {
273+
t.Fatal(err)
274+
}
275+
276+
if a2.Client.ID != clt.ID || a2.User.Login != "username" {
277+
t.Errorf("Authorization does not match client or user")
278+
}
279+
280+
if a2.Scope != "basic_profile email" {
281+
t.Errorf("Authorization scope does not match what was requested")
282+
}
283+
}
284+
248285
func TestClientCredentials(t *testing.T) {
249286
p, clt, srv := setupProvider()
250287
defer srv.Close()

0 commit comments

Comments
 (0)