2020-11-02 06:39:12 +13:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-03-14 09:44:38 +13:00
|
|
|
"strings"
|
2021-03-18 02:09:10 +13:00
|
|
|
"time"
|
2020-11-02 06:39:12 +13:00
|
|
|
|
2020-11-02 08:23:09 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2020-11-02 06:39:12 +13:00
|
|
|
)
|
|
|
|
|
2021-03-18 02:09:10 +13:00
|
|
|
func (manager *SessionManagerCtx) CookieSetToken(w http.ResponseWriter, token string) {
|
|
|
|
sameSite := http.SameSiteDefaultMode
|
|
|
|
if manager.config.CookieSecure {
|
|
|
|
sameSite = http.SameSiteNoneMode
|
|
|
|
}
|
|
|
|
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: manager.config.CookieName,
|
|
|
|
Value: token,
|
|
|
|
Expires: manager.config.CookieExpiration,
|
|
|
|
Secure: manager.config.CookieSecure,
|
|
|
|
SameSite: sameSite,
|
|
|
|
HttpOnly: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *SessionManagerCtx) CookieClearToken(w http.ResponseWriter) {
|
|
|
|
sameSite := http.SameSiteDefaultMode
|
|
|
|
if manager.config.CookieSecure {
|
|
|
|
sameSite = http.SameSiteNoneMode
|
|
|
|
}
|
|
|
|
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: manager.config.CookieName,
|
|
|
|
Value: "",
|
|
|
|
Expires: time.Unix(0, 0),
|
|
|
|
Secure: manager.config.CookieSecure,
|
|
|
|
SameSite: sameSite,
|
|
|
|
HttpOnly: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-14 08:42:56 +13:00
|
|
|
func (manager *SessionManagerCtx) Authenticate(r *http.Request) (types.Session, error) {
|
2021-03-18 02:09:10 +13:00
|
|
|
token, ok := manager.getToken(r)
|
2020-11-28 07:59:54 +13:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no authentication provided")
|
2020-11-02 06:39:12 +13:00
|
|
|
}
|
|
|
|
|
2021-03-14 10:17:49 +13:00
|
|
|
session, ok := manager.GetByToken(token)
|
2020-11-28 07:59:54 +13:00
|
|
|
if !ok {
|
2021-03-14 08:42:56 +13:00
|
|
|
return nil, fmt.Errorf("session not found")
|
2020-12-07 06:48:50 +13:00
|
|
|
}
|
|
|
|
|
2021-03-15 07:58:15 +13:00
|
|
|
if !session.Profile().CanLogin {
|
|
|
|
return nil, fmt.Errorf("login disabled")
|
|
|
|
}
|
|
|
|
|
2020-11-28 07:59:54 +13:00
|
|
|
return session, nil
|
2020-11-02 06:39:12 +13:00
|
|
|
}
|
2020-11-29 03:22:04 +13:00
|
|
|
|
2021-03-18 02:09:10 +13:00
|
|
|
func (manager *SessionManagerCtx) getToken(r *http.Request) (string, bool) {
|
2021-03-14 08:42:56 +13:00
|
|
|
// get from Header
|
|
|
|
reqToken := r.Header.Get("Authorization")
|
|
|
|
splitToken := strings.Split(reqToken, "Bearer ")
|
|
|
|
if len(splitToken) == 2 {
|
|
|
|
return strings.TrimSpace(splitToken[1]), true
|
2020-11-29 03:22:04 +13:00
|
|
|
}
|
|
|
|
|
2021-03-16 01:01:35 +13:00
|
|
|
// get from Cookie
|
2021-03-18 02:09:10 +13:00
|
|
|
cookie, err := r.Cookie(manager.config.CookieName)
|
2021-03-16 01:01:35 +13:00
|
|
|
if err == nil {
|
|
|
|
return cookie.Value, true
|
|
|
|
}
|
|
|
|
|
2021-03-14 08:42:56 +13:00
|
|
|
// get from URL
|
|
|
|
token := r.URL.Query().Get("token")
|
|
|
|
if token != "" {
|
|
|
|
return token, true
|
2020-11-29 03:22:04 +13:00
|
|
|
}
|
|
|
|
|
2021-03-14 08:42:56 +13:00
|
|
|
return "", false
|
2021-02-15 02:40:17 +13:00
|
|
|
}
|