-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgenerate.go
58 lines (50 loc) · 1.43 KB
/
generate.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
package main
import (
"context"
"fmt"
"time"
"github.com/beeper/mac-registration-provider/nac"
"github.com/beeper/mac-registration-provider/requests"
)
var globalCert []byte
func InitFetchCert(ctx context.Context) error {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
var err error
globalCert, err = requests.FetchCert(ctx)
if err != nil {
return fmt.Errorf("failed to fetch cert: %w", err)
}
return nil
}
func InitSanityCheck() error {
defer nac.MeowMemory()()
return nac.SanityCheck()
}
const ValidityTime = 15 * time.Minute
func GenerateValidationData(ctx context.Context) ([]byte, time.Time, error) {
defer nac.MeowMemory()()
validationCtx, request, err := nac.Init(globalCert)
if err != nil {
return nil, time.Time{}, err
}
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
// Record valid until time before request, so it's definitely valid for at least that long
validUntil := time.Now().UTC().Add(ValidityTime)
sessionInfo, err := requests.InitializeValidation(ctx, request)
cancel()
if err != nil {
return nil, validUntil, fmt.Errorf("failed to initialize validation: %w", err)
}
err = nac.KeyEstablishment(validationCtx, sessionInfo)
if err != nil {
return nil, validUntil, err
}
validationData, err := nac.Sign(validationCtx)
if err != nil {
return nil, validUntil, err
}
return validationData, validUntil, nil
}