-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth_adapter.go
50 lines (44 loc) · 1.31 KB
/
auth_adapter.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
// Package to request and verify jwt token
package adapter
import (
"encoding/json"
"fmt"
"net/http"
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
)
type (
//Interface for authentication
AuthAdapter interface {
RefreshToken(userId string, token string, refreshToken string) (*TokenResponseDTO, error)
GetToken(userId string, password string) (*TokenResponseDTO, error)
}
//Claim with data from the token
Claims struct {
UserId uuid.UUID `json:"user_id"`
jwt.StandardClaims
}
//Response with token data
TokenResponseDTO struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
RefreshExpiresIn int `json:"refresh_expires_in"`
}
//Request object for authentication
AuthenticateDTO struct {
Password string `json:"password" validate:"required"`
}
)
// Method to handle response with token
func readTokenResponse(resp *http.Response) (*TokenResponseDTO, error) {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w but: %v", errStatusNotOk, resp.StatusCode)
}
tokenResponse := new(TokenResponseDTO)
if err := json.NewDecoder(resp.Body).Decode(tokenResponse); err != nil {
return nil, fmt.Errorf("%w: %v", errReadResponse, err)
}
return tokenResponse, nil
}