-
Notifications
You must be signed in to change notification settings - Fork 8
/
psu_consent_url.go
98 lines (82 loc) · 2.31 KB
/
psu_consent_url.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
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
package authentication
import (
"fmt"
"net/url"
"github.com/dgrijalva/jwt-go"
)
type PSUConsentClaims struct {
AuthorizationEndpoint string
Aud string // Audience
Iss string // ClientID
ResponseType string // "code id_token"
Scope string // "openid accounts"
RedirectURI string
ConsentId string
State string // {test_id}
}
// PSUURLGenerate generates a PSU Consent URL based on claims
func PSUURLGenerate(claims PSUConsentClaims) (*url.URL, error) {
token, err := createAlgNoneJWT(claims)
if err != nil {
return nil, fmt.Errorf("generating psu consent URL: %w", err)
}
consentUrl, err := url.Parse(claims.AuthorizationEndpoint)
if err != nil {
return nil, fmt.Errorf("generating psu consent URL: %w", err)
}
consentUrlQuery := consentUrl.Query()
consentUrlQuery.Set("client_id", claims.Iss)
consentUrlQuery.Set("response_type", claims.ResponseType)
consentUrlQuery.Set("scope", claims.Scope)
consentUrlQuery.Set("request", token)
consentUrlQuery.Set("state", claims.State)
consentUrl.RawQuery = consentUrlQuery.Encode()
return consentUrl, nil
}
func createAlgNoneJWT(claims PSUConsentClaims) (string, error) {
alg := jwt.SigningMethodNone
if alg == nil {
return "", fmt.Errorf("no signing method: %v", alg)
}
token := &jwt.Token{
Header: map[string]interface{}{
"alg": alg.Alg(),
},
Claims: makeOpenBankingJWTClaims(claims),
Method: alg,
}
tokenString, err := token.SigningString()
if err != nil {
return "", err
}
// alg none might cause problems for some JWT parses so we add the "."
// to terminate the jwt
tokenString += "."
return tokenString, nil
}
type openBankingClaims struct {
IdToken idToken `json:"id_token,omitempty"`
}
type idToken struct {
IntentID intentId `json:"openbanking_intent_id,omitempty"`
}
type intentId struct {
Essential bool `json:"essential"`
Value string `json:"value"`
}
func makeOpenBankingJWTClaims(claims PSUConsentClaims) jwt.MapClaims {
return jwt.MapClaims{
"iss": claims.Iss,
"scope": claims.Scope,
"aud": claims.Aud,
"redirect_uri": claims.RedirectURI,
"claims": openBankingClaims{
IdToken: idToken{
IntentID: intentId{
Essential: true,
Value: claims.ConsentId,
},
},
},
}
}