-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
69 lines (50 loc) · 1.72 KB
/
token.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
package authn
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// Client holds some information about the client.
type Client struct {
// User agent of the client.
UserAgent string `bson:"agent,omitempty"`
// IP address of the client.
IP string `bson:"ip,omitempty"`
// At tells when the token was accessed.
At time.Time `bson:"at"`
// Data may hold arbitrary data.
Data map[string]any `bson:"data,omitempty"`
}
// Token represents a token which authenticates users.
type Token struct {
// ID of the token.
ID primitive.ObjectID `bson:"_id,omitempty"`
// Case-sensitive email of the owner of the token.
Email string `bson:"email"`
// Lowercased email of the owner of the token.
// Used for lookups.
LoweredEmail string `bson:"lemail"`
// Token creation timestamp.
Created time.Time `bson:"c"`
// One-time entry code for the token.
EntryCode string `bson:"ecode"`
// EntryClient is the client information of the entry code verification.
EntryClient *Client `bson:"eclient,omitempty"`
// Verified tells if the token's entry code has been verified.
Verified bool `bson:"verified"`
// UserID is the ID of the owner of the token.
UserID primitive.ObjectID `bson:"userID"`
// Client information of the last access.
Client *Client `bson:"client,omitempty"`
// Expires tells when this entry code or token expires.
Expires time.Time `bson:"exp"`
// Reusable token value for authentication.
Value string `bson:"value"`
// Used tells how many times this token was used.
// A token is used when it is verified, and only
// if Client information is provided.
Used int `bson:"used,omitempty"`
}
// Expired tells if this token has expired.
func (t *Token) Expired() bool {
return time.Now().After(t.Expires)
}