b9206b1844
This change allows you to host the vuetify app on <x>.example.com where the main app is hosted on example.com. A configuration is added to specify an exact subdomain for cookies. For example, if my production app is hosted on us1.storj.io and my vuetify app is hosted on vuetify.us1.storj.io, the cookie domain should be set to ".us1.storj.io" so that any authentication cookie is accessible to lower-level subdomains. Since the vuetify app does not currently support login/signup on its own, it is still required to first login to the main satellite UI, then navigate to the Vuetify app after the session cookie is set. If the "vuetifypoc" prefix is not desirable when using subdomain hosting for vuetify, the VITE_VUETIFY_PREFIX variable can be modified in web/satellite/.env before running `npm run build-vuetify`. For now, we should keep this prefix because it makes developing on the vuetify app significantly easier if subdomains are not being used. Issue: https://github.com/storj/storj/issues/6144 Change-Id: Iba1a5737892c8ee8f38148a17b94e3222f8798e6
82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package consolewebauth
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
"storj.io/storj/satellite/console/consoleauth"
|
|
)
|
|
|
|
// CookieSettings variable cookie settings.
|
|
type CookieSettings struct {
|
|
Name string
|
|
Path string
|
|
}
|
|
|
|
// CookieAuth handles cookie authorization.
|
|
type CookieAuth struct {
|
|
settings CookieSettings
|
|
domain string
|
|
}
|
|
|
|
// NewCookieAuth create new cookie authorization with provided settings.
|
|
func NewCookieAuth(settings CookieSettings, domain string) *CookieAuth {
|
|
return &CookieAuth{
|
|
settings: settings,
|
|
domain: domain,
|
|
}
|
|
}
|
|
|
|
// GetToken retrieves token from request.
|
|
func (auth *CookieAuth) GetToken(r *http.Request) (console.TokenInfo, error) {
|
|
cookie, err := r.Cookie(auth.settings.Name)
|
|
if err != nil {
|
|
return console.TokenInfo{}, err
|
|
}
|
|
|
|
token, err := consoleauth.FromBase64URLString(cookie.Value)
|
|
if err != nil {
|
|
return console.TokenInfo{}, err
|
|
}
|
|
|
|
return console.TokenInfo{
|
|
Token: token,
|
|
ExpiresAt: cookie.Expires,
|
|
}, nil
|
|
}
|
|
|
|
// SetTokenCookie sets parametrized token cookie that is not accessible from js.
|
|
func (auth *CookieAuth) SetTokenCookie(w http.ResponseWriter, tokenInfo console.TokenInfo) {
|
|
http.SetCookie(w, &http.Cookie{
|
|
Domain: auth.domain,
|
|
Name: auth.settings.Name,
|
|
Value: tokenInfo.Token.String(),
|
|
Path: auth.settings.Path,
|
|
Expires: tokenInfo.ExpiresAt,
|
|
HttpOnly: true,
|
|
SameSite: http.SameSiteStrictMode,
|
|
})
|
|
}
|
|
|
|
// RemoveTokenCookie removes auth cookie that is not accessible from js.
|
|
func (auth *CookieAuth) RemoveTokenCookie(w http.ResponseWriter) {
|
|
http.SetCookie(w, &http.Cookie{
|
|
Domain: auth.domain,
|
|
Name: auth.settings.Name,
|
|
Value: "",
|
|
Path: auth.settings.Path,
|
|
Expires: time.Unix(0, 0),
|
|
HttpOnly: true,
|
|
SameSite: http.SameSiteStrictMode,
|
|
})
|
|
}
|
|
|
|
// GetTokenCookieName returns the name of the cookie storing the session token.
|
|
func (auth *CookieAuth) GetTokenCookieName() string {
|
|
return auth.settings.Name
|
|
}
|