-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathjwt_bearer.go
More file actions
134 lines (104 loc) · 2.75 KB
/
jwt_bearer.go
File metadata and controls
134 lines (104 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright © 2025 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package clients
import (
"context"
"crypto/rsa"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"github.com/go-jose/go-jose/v4"
"github.com/go-jose/go-jose/v4/jwt"
)
// #nosec:gosec G101 - False Positive
const jwtBearerGrantType = "urn:ietf:params:oauth:grant-type:jwt-bearer"
type JWTBearer struct {
tokenURL string
client *http.Client
Signer jose.Signer
}
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
ExpiresIn int64 `json:"expires_in,omitempty"`
}
type Header struct {
Algorithm string `json:"alg"`
Typ string `json:"typ"`
KeyID string `json:"kid,omitempty"`
}
type JWTBearerPayload struct {
*jwt.Claims
PrivateClaims map[string]interface{}
}
func (c *JWTBearer) SetPrivateKey(keyID string, privateKey *rsa.PrivateKey) error {
jwk := jose.JSONWebKey{Key: privateKey, KeyID: keyID, Algorithm: string(jose.RS256)}
signingKey := jose.SigningKey{
Algorithm: jose.RS256,
Key: jwk,
}
signerOptions := &jose.SignerOptions{}
signerOptions.WithType("JWT")
sig, err := jose.NewSigner(signingKey, signerOptions)
if err != nil {
return err
}
c.Signer = sig
return nil
}
func (c *JWTBearer) GetToken(ctx context.Context, payloadData *JWTBearerPayload, scope []string) (*Token, error) {
builder := jwt.Signed(c.Signer).
Claims(payloadData.Claims).
Claims(payloadData.PrivateClaims)
assertion, err := builder.Serialize()
if err != nil {
return nil, err
}
requestBodyReader, err := c.getRequestBodyReader(assertion, scope)
if err != nil {
return nil, err
}
request, err := http.NewRequestWithContext(ctx, "POST", c.tokenURL, requestBodyReader)
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := c.client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
if c := response.StatusCode; c < 200 || c > 299 {
return nil, &RequestError{
Response: response,
Body: body,
}
}
token := &Token{}
if err := json.Unmarshal(body, token); err != nil {
return nil, err
}
return token, err
}
func (c *JWTBearer) getRequestBodyReader(assertion string, scope []string) (io.Reader, error) {
data := url.Values{}
data.Set("grant_type", jwtBearerGrantType)
data.Set("assertion", string(assertion))
if len(scope) != 0 {
data.Set("scope", strings.Join(scope, " "))
}
return strings.NewReader(data.Encode()), nil
}
func NewJWTBearer(tokenURL string) *JWTBearer {
return &JWTBearer{
client: &http.Client{},
tokenURL: tokenURL,
}
}