-
Notifications
You must be signed in to change notification settings - Fork 13
/
keypairauthenticator.go
59 lines (49 loc) · 1.38 KB
/
keypairauthenticator.go
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
package oauth
import (
"crypto/rsa"
"fmt"
"net/url"
"time"
"github.com/Axway/agent-sdk/pkg/util"
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
)
type keyPairAuthenticator struct {
clientID string
aud string
privateKey *rsa.PrivateKey
publicKey []byte
}
// prepareInitialToken prepares a token for an access request
func (p *keyPairAuthenticator) prepareInitialToken(privateKey interface{}, kid, clientID, aud string) (string, error) {
now := time.Now()
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.StandardClaims{
Issuer: fmt.Sprintf("%s:%s", assertionTypeJWT, clientID),
Subject: clientID,
Audience: aud,
ExpiresAt: now.Add(60*time.Second).UnixNano() / 1e9,
IssuedAt: now.UnixNano() / 1e9,
Id: uuid.New().String(),
})
token.Header["kid"] = kid
requestToken, err := token.SignedString(privateKey)
if err != nil {
return "", err
}
return requestToken, nil
}
func (p *keyPairAuthenticator) prepareRequest() (url.Values, error) {
kid, err := util.ComputeKIDFromDER(p.publicKey)
if err != nil {
return nil, err
}
requestToken, err := p.prepareInitialToken(p.privateKey, kid, p.clientID, p.aud)
if err != nil {
return nil, err
}
return url.Values{
metaGrantType: []string{grantClientCredentials},
metaClientAssertionType: []string{assertionTypeJWT},
metaClientAssertion: []string{requestToken},
}, nil
}