-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
34 lines (28 loc) · 751 Bytes
/
auth.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
package security
import (
"time"
"github.com/brunodmartins/church-members-api/internal/constants/domain"
"github.com/golang-jwt/jwt/v4"
"github.com/spf13/viper"
)
type Claim struct {
ID string `json:"id"`
UserName string `json:"username"`
Church *domain.Church
jwt.RegisteredClaims
}
func newClaim(user *domain.User) *Claim {
return &Claim{
ID: user.ID,
UserName: user.UserName,
Church: user.Church,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: getExpirationTime(),
Issuer: "church-members-api",
},
}
}
func getExpirationTime() *jwt.NumericDate {
hoursToExpire := viper.GetInt("security.token.expiration")
return jwt.NewNumericDate(time.Now().Add(time.Duration(hoursToExpire) * time.Hour))
}