-
Notifications
You must be signed in to change notification settings - Fork 2
/
identity_with_one_login_callback.go
103 lines (83 loc) · 3.31 KB
/
identity_with_one_login_callback.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
package certificateprovider
import (
"net/http"
"time"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/date"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type identityWithOneLoginCallbackData struct {
App page.AppData
Errors validation.List
FirstNames string
LastName string
DateOfBirth date.Date
ConfirmedAt time.Time
CouldNotConfirm bool
}
func IdentityWithOneLoginCallback(tmpl template.Template, oneLoginClient OneLoginClient, sessionStore SessionStore, certificateProviderStore CertificateProviderStore, lpaStoreResolvingService LpaStoreResolvingService, lpaStoreClient LpaStoreClient) page.Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request) error {
certificateProvider, err := certificateProviderStore.Get(r.Context())
if err != nil {
return err
}
lpa, err := lpaStoreResolvingService.Get(r.Context())
if err != nil {
return err
}
if r.Method == http.MethodPost {
if certificateProvider.CertificateProviderIdentityConfirmed(lpa.CertificateProvider.FirstNames, lpa.CertificateProvider.LastName) {
return page.Paths.CertificateProvider.ReadTheLpa.Redirect(w, r, appData, certificateProvider.LpaID)
} else {
return page.Paths.CertificateProvider.ProveYourIdentity.Redirect(w, r, appData, certificateProvider.LpaID)
}
}
data := &identityWithOneLoginCallbackData{App: appData}
if certificateProvider.CertificateProviderIdentityConfirmed(lpa.CertificateProvider.FirstNames, lpa.CertificateProvider.LastName) {
data.FirstNames = certificateProvider.IdentityUserData.FirstNames
data.LastName = certificateProvider.IdentityUserData.LastName
data.DateOfBirth = certificateProvider.IdentityUserData.DateOfBirth
data.ConfirmedAt = certificateProvider.IdentityUserData.RetrievedAt
return tmpl(w, data)
}
if r.FormValue("error") == "access_denied" {
data.CouldNotConfirm = true
return tmpl(w, data)
}
oneLoginSession, err := sessionStore.OneLogin(r)
if err != nil {
return err
}
_, accessToken, err := oneLoginClient.Exchange(r.Context(), r.FormValue("code"), oneLoginSession.Nonce)
if err != nil {
return err
}
userInfo, err := oneLoginClient.UserInfo(r.Context(), accessToken)
if err != nil {
return err
}
userData, err := oneLoginClient.ParseIdentityClaim(r.Context(), userInfo)
if err != nil {
return err
}
certificateProvider.IdentityUserData = userData
certificateProvider.Tasks.ConfirmYourIdentity = actor.TaskCompleted
if certificateProvider.CertificateProviderIdentityConfirmed(lpa.CertificateProvider.FirstNames, lpa.CertificateProvider.LastName) {
data.FirstNames = userData.FirstNames
data.LastName = userData.LastName
data.DateOfBirth = userData.DateOfBirth
data.ConfirmedAt = userData.RetrievedAt
if err := certificateProviderStore.Put(r.Context(), certificateProvider); err != nil {
return err
}
if err := lpaStoreClient.SendCertificateProviderConfirmIdentity(r.Context(), lpa.LpaUID, certificateProvider); err != nil {
return err
}
} else {
data.CouldNotConfirm = true
}
return tmpl(w, data)
}
}