@@ -104,6 +104,18 @@ func (ja *Config) AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) strin
104
104
105
105
// Exchange includes the client_assertion signed JWT.
106
106
func (ja * Config ) Exchange (ctx context.Context , code string , opts ... oauth2.AuthCodeOption ) (* oauth2.Token , error ) {
107
+ signed , err := ja .jwtToken ()
108
+ if err != nil {
109
+ return nil , xerrors .Errorf ("failed jwt assertion: %w" , err )
110
+ }
111
+ opts = append (opts ,
112
+ oauth2 .SetAuthURLParam ("client_assertion_type" , "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" ),
113
+ oauth2 .SetAuthURLParam ("client_assertion" , signed ),
114
+ )
115
+ return ja .cfg .Exchange (ctx , code , opts ... )
116
+ }
117
+
118
+ func (ja * Config ) jwtToken () (string , error ) {
107
119
now := time .Now ()
108
120
token := jwt .NewWithClaims (jwt .SigningMethodRS256 , jwt.MapClaims {
109
121
"iss" : ja .clientID ,
@@ -118,16 +130,12 @@ func (ja *Config) Exchange(ctx context.Context, code string, opts ...oauth2.Auth
118
130
119
131
signed , err := token .SignedString (ja .clientKey )
120
132
if err != nil {
121
- return nil , xerrors .Errorf ("failed to sign jwt assertion: %w" , err )
133
+ return "" , xerrors .Errorf ("sign jwt assertion: %w" , err )
122
134
}
123
-
124
- opts = append (opts ,
125
- oauth2 .SetAuthURLParam ("client_assertion_type" , "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" ),
126
- oauth2 .SetAuthURLParam ("client_assertion" , signed ),
127
- )
128
- return ja .cfg .Exchange (ctx , code , opts ... )
135
+ return signed , nil
129
136
}
130
137
131
138
func (ja * Config ) TokenSource (ctx context.Context , token * oauth2.Token ) oauth2.TokenSource {
139
+ // TODO: Hijack the http.Client to insert proper client auth assertions.
132
140
return ja .cfg .TokenSource (ctx , token )
133
141
}
0 commit comments