-
Notifications
You must be signed in to change notification settings - Fork 0
/
webstate.go
43 lines (34 loc) · 893 Bytes
/
webstate.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
// Content managed by Project Forge, see [projectforge.md] for details.
package auth
import (
"encoding/base64"
"net/url"
"github.com/markbates/goth"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"admini.dev/admini/app/util"
)
func setState(rc *fasthttp.RequestCtx) string {
state := rc.QueryArgs().Peek("state")
if len(state) > 0 {
return string(state)
}
nonceBytes := util.RandomBytes(64)
return base64.URLEncoding.EncodeToString(nonceBytes)
}
func validateState(rc *fasthttp.RequestCtx, sess goth.Session) error {
rawAuthURL, err := sess.GetAuthURL()
if err != nil {
return err
}
authURL, err := url.Parse(rawAuthURL)
if err != nil {
return err
}
originalState := authURL.Query().Get("state")
qs := string(rc.QueryArgs().Peek("state"))
if originalState != "" && (originalState != qs) {
return errors.New("state token mismatch")
}
return nil
}