-
Notifications
You must be signed in to change notification settings - Fork 5
/
session.go
73 lines (66 loc) · 1.57 KB
/
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
package auth
import (
"context"
"net/http"
"time"
)
// Session token's duration (5 minutes less actually)
const TokenDuration = 25 * time.Minute
// Session function authenticates user to eclass, stores session to the client provided
// and ensures that session will not expire. Returns the http.Client to be used, in case
// no client was provided.
func Session(
ctx context.Context,
domainURL string,
creds Credentials,
client *http.Client,
) (*http.Client, error) {
client, err := Login(ctx, domainURL, creds, client)
if err != nil {
return nil, err
}
ticker := time.NewTimer(TokenDuration)
go func() {
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := refreshToken(ctx, domainURL, creds, client)
if err != nil {
return
}
}
}
}()
return client, nil
}
// refreshToken function attempts to refresh an existing session. Eclass session token expires
// after 30 minutes. Every 25 minutes, refreshToken is invoked by Session function, thus 5 minutes
// remain to expire. If we successfully HEAD homepage within this time, the token gets refreshed.
// If not, we login to create a new one.
func refreshToken(
ctx context.Context,
domainURL string,
creds Credentials,
client *http.Client,
) error {
for {
select {
case <-ctx.Done():
return nil
case <-time.After(5 * time.Minute):
_, err := Login(ctx, domainURL, creds, client)
if err != nil {
return err
}
return nil
default:
err := headHomepage(ctx, domainURL, client)
if err == nil {
return nil
}
time.Sleep(10 * time.Second)
}
}
}