-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth-session.go
131 lines (119 loc) · 3.95 KB
/
auth-session.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2023 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import (
"net/http"
"strings"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/iancoleman/strcase"
"github.com/go-enjin/be/pkg/context"
"github.com/go-enjin/be/pkg/errors"
"github.com/go-enjin/be/pkg/feature"
"github.com/go-enjin/be/pkg/forms"
"github.com/go-enjin/be/pkg/log"
"github.com/go-enjin/be/pkg/userbase"
)
func (f *CFeature) MakeAuthClaims(aud, email string, ctx context.Context) (claims *feature.CSiteAuthClaims) {
expiration := time.Now().Add(f.sessionDuration)
su := f.Site().SiteUsers()
rid := su.MakeRealID(email)
eid := su.MakeEnjinID(rid)
audience := strcase.ToKebab(aud)
claims = &feature.CSiteAuthClaims{
RID: rid,
EID: eid,
Email: email,
Context: ctx,
RegisteredClaims: jwt.RegisteredClaims{
Issuer: f.KebabTag,
Subject: eid,
Audience: []string{audience},
IssuedAt: jwt.NewNumericDate(time.Now()),
NotBefore: jwt.NewNumericDate(time.Now()),
ExpiresAt: jwt.NewNumericDate(expiration),
},
}
claims.ResetFactorValueTypes()
return
}
func (f *CFeature) GenerateJWT(claims *feature.CSiteAuthClaims) (token string, err error) {
var audience string
if audience = claims.GetAudience(); audience == "" {
audience = DefaultAudience
} else if _, present := f.audienceKeys[audience]; !present {
audience = DefaultAudience
}
token, err = jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(f.audienceKeys[audience])
return
}
func (f *CFeature) VerifyJWT(r *http.Request) (claims *feature.CSiteAuthClaims, err error) {
var tokenCookie *http.Cookie
if tokenCookie, err = r.Cookie(f.jwtCookieName); err != nil {
return
}
var validCookie bool
var audience, reqToken string
if audience, reqToken, validCookie = strings.Cut(tokenCookie.Value, "="); !validCookie {
log.ErrorRF(r, "invalid cookie value received: %q", tokenCookie.Value)
err = errors.ErrBadCookie
return
} else if reqToken = forms.StrictSanitize(reqToken); reqToken == "" {
log.ErrorRF(r, "missing token")
err = errors.ErrTokenNotFound
return
} else if audience = forms.StrictCleanKebabValue(audience); audience == "" {
log.ErrorRF(r, "missing audience")
err = errors.ErrAudienceNotFound
return
} else if _, present := f.audienceKeys[audience]; !present {
log.ErrorRF(r, "unsupported audience requested: %q; falling back to %q", audience, DefaultAudience)
audience = DefaultAudience
}
claims = &feature.CSiteAuthClaims{}
var token *jwt.Token
if token, err = jwt.ParseWithClaims(reqToken, claims, func(token *jwt.Token) (jwtKey interface{}, err error) {
jwtKey = f.audienceKeys[audience]
return
}); err != nil {
return
} else if token == nil || !token.Valid {
err = errors.ErrBadRequest
return
}
claims.ResetFactorValueTypes()
return
}
func (f *CFeature) SetUserFactor(r *http.Request, claim *feature.CSiteAuthClaimsFactor) {
claims := f.getPrivateClaims(r)
claims.SetFactor(claim)
return
}
func (f *CFeature) deleteCookie(w http.ResponseWriter, name string) {
http.SetCookie(w, &http.Cookie{
Name: name,
Value: "",
Path: f.Site().SitePath(),
MaxAge: -1,
Secure: f.secureCookies,
HttpOnly: true,
})
}
func (f *CFeature) resetCurrentUser(w http.ResponseWriter, r *http.Request) (m *http.Request) {
f.deleteCookie(w, f.jwtCookieName)
m = f.setPrivateClaims(r, nil)
m = userbase.SetCurrentUser(nil, m)
m = userbase.SetCurrentPermissions(m, f.Enjin.GetPublicAccess()...)
return
}