-
Notifications
You must be signed in to change notification settings - Fork 2
/
initiate.go
56 lines (42 loc) · 950 Bytes
/
initiate.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
package req
import (
"crypto/rand"
"encoding/base64"
"net/http"
"github.com/dpb587/ssoca/auth/authn/support/oauth2/config"
"github.com/dpb587/ssoca/server/service/req"
"golang.org/x/oauth2"
)
type Initiate struct {
Config oauth2.Config
req.WithoutAdditionalAuthorization
}
var _ req.RouteHandler = Initiate{}
func (h Initiate) Route() string {
return "initiate"
}
func (h Initiate) Execute(req req.Request) error {
s := make([]byte, 32)
rand.Read(s)
state := base64.URLEncoding.EncodeToString(s)
http.SetCookie(
req.RawResponse,
&http.Cookie{
Name: config.CookieStateName,
Value: state,
},
)
clientPort := req.RawRequest.FormValue("client_port")
if clientPort != "" {
http.SetCookie(
req.RawResponse,
&http.Cookie{
Name: config.CookieClientPortName,
Value: clientPort,
},
)
}
url := h.Config.AuthCodeURL(state)
http.Redirect(req.RawResponse, req.RawRequest, url, 302)
return nil
}