-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.go
80 lines (66 loc) · 2.48 KB
/
project.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
package authen
import (
"sync/atomic"
"time"
"src.goblgobl.com/authen/storage"
"src.goblgobl.com/authen/storage/data"
"src.goblgobl.com/utils"
"src.goblgobl.com/utils/concurrent"
"src.goblgobl.com/utils/log"
)
var Projects concurrent.Map[*Project]
func init() {
Projects = concurrent.NewMap[*Project](loadProject)
}
// A project instance isn't updated. If the project is changed,
// a new instance is created.
type Project struct {
// Project-specific counter for generating the RequestId
requestId uint32
// Any log entry generate for this project should include
// the pid=$id field
logField log.Field
Id string
TOTPMax int `json:"totp_max"`
TOTPIssuer string `json:"totp_issuer"`
TOTPSetupTTL time.Duration `json:"totp_setup_ttl"`
TOTPSecretLength int `json:"totp_secret_length"`
TicketMax int `json:"ticket_max"`
TicketMaxPayloadLength int `json:"ticket_max_payload_length"`
LoginLogMax int `json:"login_log_max"`
LoginLogMaxPayloadLength int `json:"login_log_max_payload_length"`
}
func (p *Project) NextRequestId() string {
nextId := atomic.AddUint32(&p.requestId, 1)
return utils.EncodeRequestId(nextId, Config.InstanceId)
}
func loadProject(id string) (*Project, error) {
projectData, err := storage.DB.GetProject(id)
if projectData == nil || err != nil {
return nil, err
}
return NewProject(projectData, true), nil
}
func NewProject(projectData *data.Project, logProjectId bool) *Project {
id := projectData.Id
var logField log.Field
if logProjectId {
logField = log.NewField().String("pid", id).Finalize()
}
return &Project{
logField: logField,
Id: id,
TOTPMax: projectData.TOTPMax,
TOTPIssuer: projectData.TOTPIssuer,
TOTPSetupTTL: time.Duration(projectData.TOTPSetupTTL) * time.Second,
TOTPSecretLength: projectData.TOTPSecretLength,
TicketMax: projectData.TicketMax,
TicketMaxPayloadLength: projectData.TicketMaxPayloadLength,
LoginLogMax: projectData.LoginLogMax,
LoginLogMaxPayloadLength: projectData.LoginLogMaxPayloadLength,
// If we let this start at 0, then restarts are likely to produce duplicates.
// While we make no guarantees about the uniqueness of the requestId, there's
// no reason we can't help things out a little.
requestId: uint32(time.Now().Unix()),
}
}