forked from acoshift/go-firebase-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaims.go
51 lines (41 loc) · 1.19 KB
/
claims.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
package firebase
import (
"fmt"
"time"
)
// Claims is the firebase authentication token claims
type Claims struct {
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
Audience string `json:"aud,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
ExpiresAt int64 `json:"exp,omitempty"`
UserID string `json:"uid,omitempty"`
Claims interface{} `json:"claims,omitempty"`
}
// Valid implements jwt-go Claims interface
// for validates time based claims, such as IssuedAt, and ExpiresAt
// But not verify token signature and header
func (c *Claims) Valid() error {
now := time.Now().Unix()
if !c.verifyExpiresAt(now) {
delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
return fmt.Errorf("token is expired by %v", delta)
}
if !c.verifyIssuedAt(now) {
return fmt.Errorf("token used before issued")
}
return nil
}
func (c *Claims) verifyExpiresAt(now int64) bool {
return now <= c.ExpiresAt
}
func (c *Claims) verifyIssuedAt(now int64) bool {
return now >= c.IssuedAt
}
func (c *Claims) verifyAudience(aud string) bool {
return c.Audience == aud
}
func (c *Claims) verifyIssuer(iss string) bool {
return c.Issuer == iss
}