-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpires.go
More file actions
56 lines (47 loc) · 1.62 KB
/
expires.go
File metadata and controls
56 lines (47 loc) · 1.62 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
package mppx
import "time"
// Expires provides convenience functions for generating ISO 8601 expiration timestamps.
// Pass the result as the Expires field in ChallengeParams.
//
// Example:
//
// challenge, _ := mppx.NewChallenge(mppx.ChallengeParams{
// SecretKey: secretKey,
// Realm: "api.example.com",
// Expires: mppx.Minutes(5),
// // ...
// })
var Expires = expiresHelper{}
type expiresHelper struct{}
// Seconds returns an ISO 8601 timestamp n seconds from now.
func (expiresHelper) Seconds(n int) string {
return time.Now().UTC().Add(time.Duration(n) * time.Second).Format(time.RFC3339)
}
// Minutes returns an ISO 8601 timestamp n minutes from now.
func (expiresHelper) Minutes(n int) string {
return time.Now().UTC().Add(time.Duration(n) * time.Minute).Format(time.RFC3339)
}
// Hours returns an ISO 8601 timestamp n hours from now.
func (expiresHelper) Hours(n int) string {
return time.Now().UTC().Add(time.Duration(n) * time.Hour).Format(time.RFC3339)
}
// Days returns an ISO 8601 timestamp n days from now.
func (expiresHelper) Days(n int) string {
return time.Now().UTC().AddDate(0, 0, n).Format(time.RFC3339)
}
// Weeks returns an ISO 8601 timestamp n weeks from now.
func (expiresHelper) Weeks(n int) string {
return time.Now().UTC().AddDate(0, 0, n*7).Format(time.RFC3339)
}
// IsExpired reports whether an ISO 8601 expiration timestamp has passed.
// Returns false if the timestamp is empty (no expiration).
func IsExpired(expires string) bool {
if expires == "" {
return false
}
t, err := time.Parse(time.RFC3339, expires)
if err != nil {
return false
}
return time.Now().UTC().After(t)
}