/
auth.go
85 lines (72 loc) · 2.08 KB
/
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
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
74
75
76
77
78
79
80
81
82
83
84
85
package model
import (
"regexp"
"time"
)
var (
// KeyJWTValidAccess is context key identifier for valid jwt token
KeyJWTValidAccess = "ValidJWTAccess"
// IsAllowedEmailInput is regex validator to allowing only valid email
IsAllowedEmailInput *regexp.Regexp
)
type (
// AuthDetails consist data authorized users
AuthUserDetails struct {
UserID int `db:"user_id" json:"-"`
FullName string `db:"full_name" json:"full_name"`
Email string `db:"email" json:"email"`
RoleID int `db:"id" json:"role_id"`
RoleName string `db:"name" json:"role_name"`
Password string `db:"password" json:"-"`
}
// CreateUserRequest consist data for creating a user
CreateUserRequest struct {
FullName string `json:"full_name"`
Email string `json:"email"`
Password string `json:"password"`
}
// LoginRequest consist data for log-in a user
LoginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
// GoogleOauthResponse consist data from response google services user info
GoogleOauthResponse struct {
ID string `json:"id"`
Email string `json:"email"`
VerifiedEmail bool `json:"verified_email"`
Name string `json:"name,omitempty"`
GivenName string `json:"given_name,omitempty"`
Picture string `json:"picture,omitempty"`
Locale string `json:"locale"`
Time string `json:"time"`
}
// SuccessLoginResponse consist data of success login
SuccessLoginResponse struct {
AccessToken string `json:"access_token"`
ExpiredAt time.Time `json:"expired_at"`
Role string `json:"role"`
}
)
const (
// RoleAuthor mapping id of role author
RoleAuthor int = 2
// RoleGuest mapping id of role guest
RoleGuest int = 3
//OauthStateKey consists template key of oauth state for redis
OauthStateKey string = "oauth_state"
)
func init() {
IsAllowedEmailInput = regexp.MustCompile(`^[^\s@]+@([^\s@.,]+\.)+[^\s@.,]{2,}$`)
}
// GetValidRoleByID will return valid role by its id
func GetValidRoleByID(roleID int) (string,error) {
switch roleID {
case RoleAuthor:
return "Author",nil
case RoleGuest:
return "Guest",nil
default:
return "",ErrRoleNotExisted
}
}