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
| package auth
import ( "encoding/base64" "net/url"
"github.com/markbates/goth" "github.com/pkg/errors" "github.com/valyala/fasthttp"
"{{{ .Package }}}/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 }
|