Co-authored-by: orchestrator-agent <20+orchestrator-agent@code.cubecraftcreations.com>
This commit was merged in pull request #130.
This commit is contained in:
+77
-25
@@ -27,38 +27,86 @@ type loginState struct {
|
||||
Nonce string `json:"n"`
|
||||
Verifier string `json:"v"`
|
||||
Return string `json:"r"`
|
||||
Expires int64 `json:"e"` // Unix seconds
|
||||
FlowExpiry
|
||||
}
|
||||
|
||||
// loginCookie signs and verifies the login cookie with HMAC-SHA256.
|
||||
// loginCookie is the FlowCookie carrying loginState.
|
||||
type loginCookie struct {
|
||||
key []byte
|
||||
flow FlowCookie
|
||||
}
|
||||
|
||||
func newLoginCookie(secretKey []byte) loginCookie {
|
||||
mac := hmac.New(sha256.New, secretKey)
|
||||
mac.Write([]byte(loginCookieKeyLabel))
|
||||
return loginCookie{key: mac.Sum(nil)}
|
||||
return loginCookie{flow: NewFlowCookie(loginCookieName, loginCookieKeyLabel, loginCookieTTL, secretKey)}
|
||||
}
|
||||
|
||||
func (c loginCookie) sign(payload string) string {
|
||||
func (c loginCookie) set(w http.ResponseWriter, s loginState, now time.Time) error {
|
||||
return c.flow.Set(w, &s, now)
|
||||
}
|
||||
|
||||
func (c loginCookie) clear(w http.ResponseWriter) { c.flow.Clear(w) }
|
||||
|
||||
func (c loginCookie) read(r *http.Request, now time.Time) (loginState, error) {
|
||||
var s loginState
|
||||
if err := c.flow.Read(r, &s, now); err != nil {
|
||||
return loginState{}, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// FlowExpiry is embedded in every value a FlowCookie carries; Set fills it
|
||||
// and Read enforces it.
|
||||
type FlowExpiry struct {
|
||||
Expires int64 `json:"e"` // Unix seconds
|
||||
}
|
||||
|
||||
func (e *FlowExpiry) flowExpiry() *FlowExpiry { return e }
|
||||
|
||||
// flowValue is a pointer to a struct that embeds FlowExpiry.
|
||||
type flowValue interface {
|
||||
flowExpiry() *FlowExpiry
|
||||
}
|
||||
|
||||
// FlowCookie carries a redirect flow's state — the OAuth state, PKCE
|
||||
// verifier and the like — from the request that sends the browser to a
|
||||
// provider to the callback it returns to. The value is JSON, signed with
|
||||
// HMAC-SHA256 under a key derived from the server secret and the cookie's
|
||||
// own label, and expires after a fixed time. It is not encrypted: it must
|
||||
// not hold anything the browser may not see.
|
||||
type FlowCookie struct {
|
||||
name string
|
||||
ttl time.Duration
|
||||
key []byte
|
||||
}
|
||||
|
||||
// NewFlowCookie returns the flow cookie name, valid for ttl, signed under a
|
||||
// key derived from secretKey and keyLabel. Each flow uses its own keyLabel so
|
||||
// one flow's cookie never verifies as another's. name must carry the __Host-
|
||||
// prefix: the cookie is Secure, host-only and Path=/.
|
||||
func NewFlowCookie(name, keyLabel string, ttl time.Duration, secretKey []byte) FlowCookie {
|
||||
mac := hmac.New(sha256.New, secretKey)
|
||||
mac.Write([]byte(keyLabel))
|
||||
return FlowCookie{name: name, ttl: ttl, key: mac.Sum(nil)}
|
||||
}
|
||||
|
||||
func (c FlowCookie) sign(payload string) string {
|
||||
mac := hmac.New(sha256.New, c.key)
|
||||
mac.Write([]byte(payload))
|
||||
return base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
|
||||
func (c loginCookie) set(w http.ResponseWriter, s loginState, now time.Time) error {
|
||||
s.Expires = now.Add(loginCookieTTL).Unix()
|
||||
data, err := json.Marshal(s)
|
||||
// Set stores v, which expires ttl after now, in the cookie.
|
||||
func (c FlowCookie) Set(w http.ResponseWriter, v flowValue, now time.Time) error {
|
||||
v.flowExpiry().Expires = now.Add(c.ttl).Unix()
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payload := base64.RawURLEncoding.EncodeToString(data)
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: loginCookieName,
|
||||
Name: c.name,
|
||||
Value: payload + "." + c.sign(payload),
|
||||
Path: "/",
|
||||
MaxAge: int(loginCookieTTL / time.Second),
|
||||
MaxAge: int(c.ttl / time.Second),
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
// Lax so the cookie rides the provider's top-level redirect back.
|
||||
@@ -67,9 +115,12 @@ func (c loginCookie) set(w http.ResponseWriter, s loginState, now time.Time) err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c loginCookie) clear(w http.ResponseWriter) {
|
||||
// Clear asks the browser to discard the cookie. A copy of it still verifies
|
||||
// until it expires, so a flow that must be single-use also consumes its
|
||||
// state on the server.
|
||||
func (c FlowCookie) Clear(w http.ResponseWriter) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: loginCookieName,
|
||||
Name: c.name,
|
||||
Path: "/",
|
||||
MaxAge: -1,
|
||||
Secure: true,
|
||||
@@ -78,27 +129,28 @@ func (c loginCookie) clear(w http.ResponseWriter) {
|
||||
})
|
||||
}
|
||||
|
||||
func (c loginCookie) read(r *http.Request, now time.Time) (loginState, error) {
|
||||
cookie, err := r.Cookie(loginCookieName)
|
||||
// Read verifies the cookie r carries and decodes it into v. It fails when
|
||||
// the cookie is missing, its signature is invalid or it has expired at now.
|
||||
func (c FlowCookie) Read(r *http.Request, v flowValue, now time.Time) error {
|
||||
cookie, err := r.Cookie(c.name)
|
||||
if err != nil {
|
||||
return loginState{}, errors.New("login cookie missing")
|
||||
return fmt.Errorf("%s cookie missing", c.name)
|
||||
}
|
||||
payload, sig, ok := strings.Cut(cookie.Value, ".")
|
||||
if !ok || !hmac.Equal([]byte(sig), []byte(c.sign(payload))) {
|
||||
return loginState{}, errors.New("login cookie signature invalid")
|
||||
return fmt.Errorf("%s cookie signature invalid", c.name)
|
||||
}
|
||||
data, err := base64.RawURLEncoding.DecodeString(payload)
|
||||
if err != nil {
|
||||
return loginState{}, fmt.Errorf("login cookie encoding: %w", err)
|
||||
return fmt.Errorf("%s cookie encoding: %w", c.name, err)
|
||||
}
|
||||
var s loginState
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return loginState{}, fmt.Errorf("login cookie payload: %w", err)
|
||||
if err := json.Unmarshal(data, v); err != nil {
|
||||
return fmt.Errorf("%s cookie payload: %w", c.name, err)
|
||||
}
|
||||
if now.Unix() >= s.Expires {
|
||||
return loginState{}, errors.New("login cookie expired")
|
||||
if now.Unix() >= v.flowExpiry().Expires {
|
||||
return fmt.Errorf("%s cookie expired", c.name)
|
||||
}
|
||||
return s, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// returnPath validates the optional post-login return path. Empty means "/".
|
||||
|
||||
Reference in New Issue
Block a user