-
Notifications
You must be signed in to change notification settings - Fork 2
/
register.go
199 lines (167 loc) · 9 KB
/
register.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package certificateprovider
import (
"context"
"io"
"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/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/identity"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/onelogin"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
"github.com/ministryofjustice/opg-modernising-lpa/internal/random"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
)
type LpaStoreResolvingService interface {
Get(ctx context.Context) (*lpastore.Lpa, error)
}
type Logger interface {
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
}
type CertificateProviderStore interface {
Create(ctx context.Context, lpaOwnerKey dynamo.LpaOwnerKeyType, certificateProviderUID actoruid.UID, email string) (*actor.CertificateProviderProvidedDetails, error)
Get(ctx context.Context) (*actor.CertificateProviderProvidedDetails, error)
Put(ctx context.Context, certificateProvider *actor.CertificateProviderProvidedDetails) error
}
type OneLoginClient interface {
AuthCodeURL(state, nonce, locale string, identity bool) (string, error)
Exchange(ctx context.Context, code, nonce string) (idToken, accessToken string, err error)
UserInfo(ctx context.Context, accessToken string) (onelogin.UserInfo, error)
ParseIdentityClaim(ctx context.Context, userInfo onelogin.UserInfo) (identity.UserData, error)
}
type ShareCodeStore interface {
Get(ctx context.Context, actorType actor.Type, shareCode string) (actor.ShareCodeData, error)
Put(ctx context.Context, actorType actor.Type, shareCode string, shareCodeData actor.ShareCodeData) error
Delete(ctx context.Context, shareCode actor.ShareCodeData) error
}
type Template func(io.Writer, interface{}) error
type SessionStore interface {
Login(r *http.Request) (*sesh.LoginSession, error)
SetLogin(r *http.Request, w http.ResponseWriter, session *sesh.LoginSession) error
OneLogin(r *http.Request) (*sesh.OneLoginSession, error)
SetOneLogin(r *http.Request, w http.ResponseWriter, session *sesh.OneLoginSession) error
}
type NotifyClient interface {
SendEmail(ctx context.Context, to string, email notify.Email) error
SendActorEmail(ctx context.Context, to, lpaUID string, email notify.Email) error
}
type ShareCodeSender interface {
SendAttorneys(context.Context, page.AppData, *lpastore.Lpa) error
}
type AddressClient interface {
LookupPostcode(ctx context.Context, postcode string) ([]place.Address, error)
}
type Localizer interface {
page.Localizer
}
type DashboardStore interface {
GetAll(ctx context.Context) (donor, attorney, certificateProvider []page.LpaAndActorTasks, err error)
SubExistsForActorType(ctx context.Context, sub string, actorType actor.Type) (bool, error)
}
type LpaStoreClient interface {
SendCertificateProvider(ctx context.Context, certificateProvider *actor.CertificateProviderProvidedDetails, lpa *lpastore.Lpa) error
}
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
func Register(
rootMux *http.ServeMux,
logger Logger,
commonTmpls, tmpls template.Templates,
sessionStore SessionStore,
oneLoginClient OneLoginClient,
shareCodeStore ShareCodeStore,
errorHandler page.ErrorHandler,
certificateProviderStore CertificateProviderStore,
notFoundHandler page.Handler,
addressClient AddressClient,
notifyClient NotifyClient,
shareCodeSender ShareCodeSender,
dashboardStore DashboardStore,
lpaStoreClient LpaStoreClient,
lpaStoreResolvingService LpaStoreResolvingService,
) {
handleRoot := makeHandle(rootMux, errorHandler)
handleRoot(page.Paths.CertificateProvider.Login,
page.Login(oneLoginClient, sessionStore, random.String, page.Paths.CertificateProvider.LoginCallback))
handleRoot(page.Paths.CertificateProvider.LoginCallback,
page.LoginCallback(oneLoginClient, sessionStore, page.Paths.CertificateProvider.EnterReferenceNumber, dashboardStore, actor.TypeCertificateProvider))
handleRoot(page.Paths.CertificateProvider.EnterReferenceNumber,
EnterReferenceNumber(tmpls.Get("enter_reference_number.gohtml"), shareCodeStore, sessionStore, certificateProviderStore))
handleCertificateProvider := makeCertificateProviderHandle(rootMux, sessionStore, errorHandler)
handleCertificateProvider(page.Paths.CertificateProvider.WhoIsEligible, page.None,
Guidance(tmpls.Get("who_is_eligible.gohtml"), lpaStoreResolvingService, nil))
handleCertificateProvider(page.Paths.CertificateProvider.TaskList, page.None,
TaskList(tmpls.Get("task_list.gohtml"), lpaStoreResolvingService, certificateProviderStore))
handleCertificateProvider(page.Paths.CertificateProvider.EnterDateOfBirth, page.CanGoBack,
EnterDateOfBirth(tmpls.Get("enter_date_of_birth.gohtml"), lpaStoreResolvingService, certificateProviderStore))
handleCertificateProvider(page.Paths.CertificateProvider.YourPreferredLanguage, page.CanGoBack,
YourPreferredLanguage(commonTmpls.Get("your_preferred_language.gohtml"), certificateProviderStore, lpaStoreResolvingService))
handleCertificateProvider(page.Paths.CertificateProvider.WhatIsYourHomeAddress, page.None,
WhatIsYourHomeAddress(logger, tmpls.Get("what_is_your_home_address.gohtml"), addressClient, certificateProviderStore))
handleCertificateProvider(page.Paths.CertificateProvider.ConfirmYourDetails, page.None,
ConfirmYourDetails(tmpls.Get("confirm_your_details.gohtml"), lpaStoreResolvingService, certificateProviderStore))
handleCertificateProvider(page.Paths.CertificateProvider.YourRole, page.CanGoBack,
Guidance(tmpls.Get("your_role.gohtml"), lpaStoreResolvingService, nil))
handleCertificateProvider(page.Paths.CertificateProvider.ProveYourIdentity, page.None,
Guidance(tmpls.Get("prove_your_identity.gohtml"), nil, nil))
handleCertificateProvider(page.Paths.CertificateProvider.IdentityWithOneLogin, page.None,
IdentityWithOneLogin(oneLoginClient, sessionStore, random.String))
handleCertificateProvider(page.Paths.CertificateProvider.IdentityWithOneLoginCallback, page.None,
IdentityWithOneLoginCallback(commonTmpls.Get("identity_with_one_login_callback.gohtml"), oneLoginClient, sessionStore, certificateProviderStore, lpaStoreResolvingService))
handleCertificateProvider(page.Paths.CertificateProvider.ReadTheLpa, page.None,
ReadTheLpa(tmpls.Get("read_the_lpa.gohtml"), lpaStoreResolvingService, certificateProviderStore))
handleCertificateProvider(page.Paths.CertificateProvider.WhatHappensNext, page.CanGoBack,
Guidance(tmpls.Get("what_happens_next.gohtml"), lpaStoreResolvingService, nil))
handleCertificateProvider(page.Paths.CertificateProvider.ProvideCertificate, page.CanGoBack,
ProvideCertificate(tmpls.Get("provide_certificate.gohtml"), lpaStoreResolvingService, certificateProviderStore, notifyClient, shareCodeSender, lpaStoreClient, time.Now))
handleCertificateProvider(page.Paths.CertificateProvider.CertificateProvided, page.None,
Guidance(tmpls.Get("certificate_provided.gohtml"), lpaStoreResolvingService, certificateProviderStore))
}
func makeHandle(mux *http.ServeMux, errorHandler page.ErrorHandler) func(page.Path, page.Handler) {
return func(path page.Path, h page.Handler) {
mux.HandleFunc(path.String(), func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
appData := page.AppDataFromContext(ctx)
appData.ActorType = actor.TypeCertificateProvider
appData.Page = path.Format()
if err := h(appData, w, r.WithContext(page.ContextWithAppData(ctx, appData))); err != nil {
errorHandler(w, r, err)
}
})
}
}
func makeCertificateProviderHandle(mux *http.ServeMux, sessionStore SessionStore, errorHandler page.ErrorHandler) func(page.CertificateProviderPath, page.HandleOpt, page.Handler) {
return func(path page.CertificateProviderPath, opt page.HandleOpt, h page.Handler) {
mux.HandleFunc(path.String(), func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
appData := page.AppDataFromContext(ctx)
appData.ActorType = actor.TypeCertificateProvider
appData.CanGoBack = opt&page.CanGoBack != 0
appData.LpaID = r.PathValue("id")
session, err := sessionStore.Login(r)
if err != nil {
http.Redirect(w, r, page.Paths.CertificateProviderStart.Format(), http.StatusFound)
return
}
appData.SessionID = session.SessionID()
sessionData, err := page.SessionDataFromContext(ctx)
if err == nil {
sessionData.SessionID = appData.SessionID
sessionData.LpaID = appData.LpaID
ctx = page.ContextWithSessionData(ctx, sessionData)
} else {
ctx = page.ContextWithSessionData(ctx, &page.SessionData{SessionID: appData.SessionID, LpaID: appData.LpaID})
}
appData.Page = path.Format(appData.LpaID)
if err := h(appData, w, r.WithContext(page.ContextWithAppData(ctx, appData))); err != nil {
errorHandler(w, r, err)
}
})
}
}