/
api_user.go
151 lines (129 loc) · 3.9 KB
/
api_user.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package api
import (
"time"
)
// UserModel Classe User representa um usuário da API e gerencia a autenticação e autorização desse usuário.
type UserModel struct {
// Atributos da classe
id string
apiKey string
email string
authUri string
tokenUri string
info string
auth string
services string
name string
role string
firstLetter string
clientId string
storage *StorageModel
}
var instance *UserModel
func User(config interface{}) *UserModel {
if instance == nil {
instance = &UserModel{}
}
instance.init(config)
return instance
}
func (au *UserModel) Storage() *StorageModel {
storage := Storage()
au.storage = storage
return storage
}
func (au *UserModel) init(value interface{}) {
security := Security()
Storage()
au.clientId = security.EncodeSha1("GUEST")
v, ok := value.(map[string]interface{})
if !ok {
return
}
if name, ok := v["name"].(string); ok && name != "" {
au.name = name
au.firstLetter = name[:1]
}
if id, ok := v["_id"].(string); ok && id != "" {
au.id = id
}
if role, ok := v["role"].(string); ok && role != "" {
au.role = role
}
if clientId, ok := v["clientId"].(string); ok && clientId != "" {
au.clientId = clientId
}
}
func (au *UserModel) ClientId() string {
return au.clientId
}
func (au *UserModel) SetClientId(id string) {
au.clientId = id
}
// Authorize Método para validar o token de autorização e obter o token de acesso e autenticação.
func (au *UserModel) Authorize() {
accessToken := au.GetAccessToken()
if accessToken == "" {
apiError := ApiError([]string{"Nenhum token de 'autorização' foi definido na inicialização da aplicação."},
"User", "Authorize", "AUTHORIZE_TOKEN_NOT_FOUND", time.Now(), accessToken)
E(apiError.ToString(), apiError.Code, nil)
panic(apiError.Code)
}
apiTokenAuthorizeUser := TokenAuthorizeUser(accessToken)
if !apiTokenAuthorizeUser.IsValid() {
panic("Token de autorização da usuário 'user' inválido.")
}
au.handleAuthorization(&apiTokenAuthorizeUser)
}
// Função para lidar com a autorização e obter o token de acesso.
func (au *UserModel) handleAuthorization(apiTokenAuthorizeUser *TokenAuthorizeUserModel) {
isAuthorizeValid := apiTokenAuthorizeUser.Authorize()
if !isAuthorizeValid {
panic("Token de autorização da usuário 'user' inválido.")
}
au.handleAccessToken(apiTokenAuthorizeUser)
}
// Função para lidar com o token de acesso e validá-lo.
func (au *UserModel) handleAccessToken(apiTokenAuthorizeUser *TokenAuthorizeUserModel) {
apiTokenAccessUser := TokenAccessUser(*apiTokenAuthorizeUser)
isTokenValid := apiTokenAccessUser.Token()
if !isTokenValid {
panic("Token de autorização da usuário 'user' inválido.")
}
}
func (au *UserModel) resume() map[string]interface{} {
return map[string]interface{}{
"_id": au.id,
"name": au.name,
}
}
// AccessToken Método para salvar o token de acesso do usuário.
func (au *UserModel) AccessToken(value string) {
au.storage.Add("accessToken", value)
}
// GetAccessToken Método para obter o token de acesso do usuário.
func (au *UserModel) GetAccessToken() string {
return au.storage.Read("accessToken")
}
// AuthToken Método para salvar o token de autenticação do usuário.
func (au *UserModel) AuthToken(value string) {
au.storage.Add("authToken", value)
}
// GetAuthToken Método para obter o token de autenticação do usuário.
func (au *UserModel) GetAuthToken() string {
return au.storage.Read("authToken")
}
// ServerUri Método para salvar o URI do servidor.
func (au *UserModel) ServerUri(value string) {
au.storage.Add("serverUri", value)
}
// GetServerUri Método para obter o URI do servidor.
func (au *UserModel) GetServerUri() string {
return au.storage.Read("serverUri")
}
func (au *UserModel) RefreshToken(value string) {
au.storage.Add("refreshToken", value)
}
func (au *UserModel) GetRefreshToken() string {
return au.storage.Read("refreshToken")
}