-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_authentication.go
153 lines (126 loc) · 4.62 KB
/
user_authentication.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
152
153
package authlib
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
// LoginInput represents the data required for login
type LoginInput struct {
Email string `json:"email"`
Password string `json:"password"`
}
// LoginOutput represents the data returned after successful login
type LoginOutput struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
// Login sends a request to the login endpoint and returns an access token and refresh token on successful login
func (c *Client) Login(ctx context.Context, input LoginInput) (*LoginOutput, error) {
loginURL := fmt.Sprintf("%s/login", c.BaseURL)
// Marshal the input into JSON
reqBody, err := json.Marshal(input)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
// Create a new request
req, err := http.NewRequestWithContext(ctx, http.MethodPost, loginURL, bytes.NewBuffer(reqBody))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
// Set the headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.ApiKey))
// Send the request
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
// If the response status is not 200, return an error
if resp.StatusCode != http.StatusOK {
return nil, errors.New("failed to login")
}
// Decode the response body
var output LoginOutput
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, fmt.Errorf("failed to decode response body: %w", err)
}
return &output, nil
}
// VerifyUserAuthentication verifies the user authentication
func (c *Client) VerifyUserAuthentication(ctx context.Context, token string) (bool, error) {
// Implementation of VerifyUserAuthentication
// Ensure this function exists and is correct
verifyURL := fmt.Sprintf("%s/verify", c.BaseURL)
reqBody, err := json.Marshal(map[string]string{"token": token})
if err != nil {
return false, fmt.Errorf("failed to marshal request body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, verifyURL, bytes.NewBuffer(reqBody))
if err != nil {
return false, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.ApiKey))
resp, err := c.HttpClient.Do(req)
if err != nil {
return false, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return false, errors.New("failed to verify user authentication")
}
var authResponse struct {
Authenticated bool `json:"authenticated"`
}
if err := json.NewDecoder(resp.Body).Decode(&authResponse); err != nil {
return false, fmt.Errorf("failed to decode response body: %w", err)
}
return authResponse.Authenticated, nil
}
// CheckUserAuthorizationInput represents the data required to check user authorization
type CheckUserAuthorizationInput struct {
UserID string `json:"user_id"`
Resource string `json:"resource"`
Action string `json:"action"`
}
// CheckUserAuthorizationOutput represents the response from the authorization check
type CheckUserAuthorizationOutput struct {
Authorized bool `json:"authorized"`
}
// CheckUserAuthorization checks if a user is authorized to perform a certain action on a resource
func (c *Client) CheckUserAuthorization(ctx context.Context, input CheckUserAuthorizationInput) (*CheckUserAuthorizationOutput, error) {
authURL := fmt.Sprintf("%s/authorize", c.BaseURL)
// Marshal the input into JSON
reqBody, err := json.Marshal(input)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
// Create a new request
req, err := http.NewRequestWithContext(ctx, http.MethodPost, authURL, bytes.NewBuffer(reqBody))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
// Set the headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.ApiKey))
// Send the request
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
// If the response status is not 200, return an error
if resp.StatusCode != http.StatusOK {
return nil, errors.New("failed to check user authorization")
}
// Decode the response body
var output CheckUserAuthorizationOutput
if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, fmt.Errorf("failed to decode response body: %w", err)
}
return &output, nil
}