-
Notifications
You must be signed in to change notification settings - Fork 2
/
user_info.go
171 lines (138 loc) · 4.51 KB
/
user_info.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package onelogin
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/ministryofjustice/opg-modernising-lpa/internal/date"
"github.com/ministryofjustice/opg-modernising-lpa/internal/identity"
)
var ErrMissingCoreIdentityJWT = errors.New("UserInfo missing CoreIdentityJWT property")
type UserInfo struct {
Sub string `json:"sub"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
Phone string `json:"phone"`
PhoneVerified bool `json:"phone_verified"`
UpdatedAt int `json:"updated_at"`
CoreIdentityJWT string `json:"https://vocab.account.gov.uk/v1/coreIdentityJWT"`
}
type CoreIdentityClaims struct {
jwt.RegisteredClaims
Vot string `json:"vot"`
Vtm string `json:"vtm"`
Vc Credential `json:"vc"`
}
type Credential struct {
Type []string `json:"type"`
CredentialSubject CredentialSubject `json:"credentialSubject"`
}
type CredentialSubject struct {
Names []CredentialName `json:"name"`
// BirthDate may list multiple values if there’s evidence an incorrect date of
// birth was previously recorded for your user. The date of birth GOV.UK One
// Login has highest confidence in will be the first item in the list.
BirthDate []CredentialBirthDate `json:"birthDate"`
}
func (s CredentialSubject) CurrentNameParts() []NamePart {
for _, name := range s.Names {
if time.Time(name.ValidUntil).IsZero() {
return name.NameParts
}
}
return nil
}
type CredentialName struct {
// ValidFrom shows when a name started to be used. If the zero value then the
// user may have used that name from birth.
ValidFrom Date `json:"validFrom"`
// ValidUntil shows when the name ceased to be used. If the zero value then
// this is the current name.
ValidUntil Date `json:"validUntil"`
// NameParts contains the components of the name in any order. The order of
// names may depend on either your user’s preferences or the order they appear
// on documents used to prove your user’s identity.
NameParts []NamePart `json:"nameParts"`
}
type CredentialBirthDate struct {
Value date.Date `json:"value"`
}
type NamePart struct {
Value string `json:"value"`
// Type is either 'GivenName' or 'FamilyName'
Type string `json:"type"`
}
type Date time.Time
func (d *Date) UnmarshalText(text []byte) error {
t, err := time.Parse("2006-01-02", string(text))
*d = Date(t)
return err
}
func (c *Client) UserInfo(ctx context.Context, idToken string) (UserInfo, error) {
endpoint, err := c.openidConfiguration.UserinfoEndpoint()
if err != nil {
return UserInfo{}, err
}
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return UserInfo{}, err
}
req.Header.Add("Authorization", "Bearer "+idToken)
res, err := c.httpClient.Do(req)
if err != nil {
return UserInfo{}, err
}
defer res.Body.Close()
var userinfoResponse UserInfo
err = json.NewDecoder(res.Body).Decode(&userinfoResponse)
return userinfoResponse, err
}
func (c *Client) ParseIdentityClaim(ctx context.Context, u UserInfo) (identity.UserData, error) {
publicKey, err := c.identityPublicKeyFunc(ctx)
if err != nil {
return identity.UserData{}, err
}
if u.CoreIdentityJWT == "" {
return identity.UserData{}, ErrMissingCoreIdentityJWT
}
token, err := jwt.ParseWithClaims(u.CoreIdentityJWT, &CoreIdentityClaims{}, func(token *jwt.Token) (any, error) {
if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
return nil, fmt.Errorf("signing method %v is invalid", token.Header["alg"])
}
return publicKey, nil
}, jwt.WithIssuedAt())
if err != nil {
return identity.UserData{}, err
}
if !token.Valid {
return identity.UserData{}, errors.New("jwt not valid")
}
claims := token.Claims.(*CoreIdentityClaims)
currentName := claims.Vc.CredentialSubject.CurrentNameParts()
if len(currentName) == 0 || claims.IssuedAt == nil {
return identity.UserData{OK: false}, nil
}
var givenName, familyName []string
for _, part := range currentName {
if part.Type == "GivenName" {
givenName = append(givenName, part.Value)
} else {
familyName = append(familyName, part.Value)
}
}
birthDates := claims.Vc.CredentialSubject.BirthDate
if len(birthDates) == 0 || !birthDates[0].Value.Valid() {
return identity.UserData{OK: false}, nil
}
return identity.UserData{
OK: true,
FirstNames: strings.Join(givenName, " "),
LastName: strings.Join(familyName, " "),
DateOfBirth: birthDates[0].Value,
RetrievedAt: claims.IssuedAt.Time,
}, nil
}