forked from 39george/authpher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_session.go
More file actions
57 lines (46 loc) · 1.22 KB
/
Copy pathauth_session.go
File metadata and controls
57 lines (46 loc) · 1.22 KB
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
package authpher
import (
"context"
"fmt"
)
// AuthSession, user-interaction type
type AuthSession[P comparable, C any] struct {
User AuthUser
backend AuthzBackend[P, C]
data Data
dataKey string
session SessionStore
}
// Verifies the provided credentials via the backend returning the
// authenticated user if valid and otherwise `nil`.
func (aS *AuthSession[T, C]) Authenticate(
ctx context.Context,
creds C,
) (AuthUser, error) {
return aS.backend.Authenticate(ctx, creds)
}
// Updates the session such that the user is logged in.
func (aS *AuthSession[P, C]) Login(c context.Context, user AuthUser) error {
if user == nil {
return fmt.Errorf("user shouldn't be nil")
}
aS.User = user
if len(aS.data.Hash) == 0 {
// TODO: perform session-fixation mitigation
// Prevent session fixation attacks by ensuring a
// new ID is assigned to the session.
}
aS.data.UserId = user.UserId()
aS.data.Hash = user.SessionAuthHash()
// Update session
aS.session.Set(c, aS.dataKey, aS.data)
aS.session.Save(c)
return nil
}
// Updates the session such that the user is logged out.
func (aS *AuthSession[T, C]) Logout(c context.Context) AuthUser {
user := aS.User
aS.User = nil
aS.session.Clear(c)
return user
}