2020-01-20 18:57:14 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consolewebauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
2022-06-05 23:41:38 +01:00
|
|
|
|
2022-07-19 10:26:18 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
2022-06-05 23:41:38 +01:00
|
|
|
"storj.io/storj/satellite/console/consoleauth"
|
2020-01-20 18:57:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CookieSettings variable cookie settings.
|
|
|
|
type CookieSettings struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// CookieAuth handles cookie authorization.
|
|
|
|
type CookieAuth struct {
|
|
|
|
settings CookieSettings
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCookieAuth create new cookie authorization with provided settings.
|
|
|
|
func NewCookieAuth(settings CookieSettings) *CookieAuth {
|
|
|
|
return &CookieAuth{
|
|
|
|
settings: settings,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetToken retrieves token from request.
|
2022-07-19 10:26:18 +01:00
|
|
|
func (auth *CookieAuth) GetToken(r *http.Request) (console.TokenInfo, error) {
|
2020-01-20 18:57:14 +00:00
|
|
|
cookie, err := r.Cookie(auth.settings.Name)
|
|
|
|
if err != nil {
|
2022-07-19 10:26:18 +01:00
|
|
|
return console.TokenInfo{}, err
|
2022-06-05 23:41:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
token, err := consoleauth.FromBase64URLString(cookie.Value)
|
|
|
|
if err != nil {
|
2022-07-19 10:26:18 +01:00
|
|
|
return console.TokenInfo{}, err
|
2020-01-20 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 10:26:18 +01:00
|
|
|
return console.TokenInfo{
|
|
|
|
Token: token,
|
|
|
|
ExpiresAt: cookie.Expires,
|
|
|
|
}, nil
|
2020-01-20 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTokenCookie sets parametrized token cookie that is not accessible from js.
|
2022-07-19 10:26:18 +01:00
|
|
|
func (auth *CookieAuth) SetTokenCookie(w http.ResponseWriter, tokenInfo console.TokenInfo) {
|
2020-01-20 18:57:14 +00:00
|
|
|
http.SetCookie(w, &http.Cookie{
|
2022-07-19 10:26:18 +01:00
|
|
|
Name: auth.settings.Name,
|
|
|
|
Value: tokenInfo.Token.String(),
|
|
|
|
Path: auth.settings.Path,
|
|
|
|
Expires: tokenInfo.ExpiresAt,
|
2020-01-20 18:57:14 +00:00
|
|
|
HttpOnly: true,
|
|
|
|
SameSite: http.SameSiteStrictMode,
|
|
|
|
})
|
|
|
|
}
|
2020-01-29 13:10:13 +00:00
|
|
|
|
|
|
|
// RemoveTokenCookie removes auth cookie that is not accessible from js.
|
|
|
|
func (auth *CookieAuth) RemoveTokenCookie(w http.ResponseWriter) {
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: auth.settings.Name,
|
|
|
|
Value: "",
|
2021-08-11 12:50:50 +01:00
|
|
|
Path: auth.settings.Path,
|
2020-01-29 13:10:13 +00:00
|
|
|
Expires: time.Unix(0, 0),
|
|
|
|
HttpOnly: true,
|
|
|
|
SameSite: http.SameSiteStrictMode,
|
|
|
|
})
|
|
|
|
}
|
2022-06-05 23:41:38 +01:00
|
|
|
|
|
|
|
// GetTokenCookieName returns the name of the cookie storing the session token.
|
|
|
|
func (auth *CookieAuth) GetTokenCookieName() string {
|
|
|
|
return auth.settings.Name
|
|
|
|
}
|