-
Notifications
You must be signed in to change notification settings - Fork 2
/
identity_with_one_login_callback.go
90 lines (73 loc) · 2.48 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
package donor
import (
"net/http"
"time"
"github.com/gorilla/sessions"
"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/sesh"
"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 sessions.Store, donorStore DonorStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
if r.Method == http.MethodPost {
if donor.DonorIdentityConfirmed() {
return page.Paths.ReadYourLpa.Redirect(w, r, appData, donor)
} else {
return page.Paths.ProveYourIdentity.Redirect(w, r, appData, donor)
}
}
data := &identityWithOneLoginCallbackData{App: appData}
if donor.DonorIdentityConfirmed() {
data.FirstNames = donor.DonorIdentityUserData.FirstNames
data.LastName = donor.DonorIdentityUserData.LastName
data.DateOfBirth = donor.DonorIdentityUserData.DateOfBirth
data.ConfirmedAt = donor.DonorIdentityUserData.RetrievedAt
return tmpl(w, data)
}
if r.FormValue("error") == "access_denied" {
data.CouldNotConfirm = true
return tmpl(w, data)
}
oneLoginSession, err := sesh.OneLogin(sessionStore, 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
}
donor.DonorIdentityUserData = userData
if donor.DonorIdentityConfirmed() {
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
data.FirstNames = userData.FirstNames
data.LastName = userData.LastName
data.DateOfBirth = userData.DateOfBirth
data.ConfirmedAt = userData.RetrievedAt
} else {
data.CouldNotConfirm = true
}
return tmpl(w, data)
}
}