-
Notifications
You must be signed in to change notification settings - Fork 2
/
login.go
45 lines (36 loc) · 1.12 KB
/
login.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
package page
import (
"net/http"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
)
type LoginOneLoginClient interface {
AuthCodeURL(state, nonce, locale string, identity bool) (string, error)
}
type LoginSessionStore interface {
SetOneLogin(r *http.Request, w http.ResponseWriter, session *sesh.OneLoginSession) error
}
func Login(oneLoginClient LoginOneLoginClient, sessionStore LoginSessionStore, randomString func(int) string, redirect Path) Handler {
return func(appData AppData, w http.ResponseWriter, r *http.Request) error {
locale := "en"
if appData.Lang == localize.Cy {
locale = "cy"
}
state := randomString(12)
nonce := randomString(12)
authCodeURL, err := oneLoginClient.AuthCodeURL(state, nonce, locale, false)
if err != nil {
return err
}
if err := sessionStore.SetOneLogin(r, w, &sesh.OneLoginSession{
State: state,
Nonce: nonce,
Locale: locale,
Redirect: redirect.Format(),
}); err != nil {
return err
}
http.Redirect(w, r, authCodeURL, http.StatusFound)
return nil
}
}