-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
88 lines (71 loc) · 2.27 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
86
87
88
package headhunter
import (
"bytes"
"fmt"
"github.com/tidwall/gjson"
"io"
"net/http"
"strconv"
)
const (
authorizeURL = "https://hh.ru/oauth/authorize?response_type=code&client_id=%s&redirect_uri=%s"
authorizeEndpoint = "https://hh.ru/oauth/token"
logoutEndpoint = "https://api.hh.ru/oauth/token"
meEndpoint = "/me"
)
type (
AuthorizeResponse struct {
AccessToken string
RefreshToken string
}
MeType struct {
Name string
LastName string
MiddleName string
Email string
Phone string
UserID string
}
)
func (c *Client) GetAuthorizationURL(chatID int64) (string, error) {
fullRedirectURL := c.redirectURI + "/?chat_id=" + strconv.Itoa(int(chatID))
return fmt.Sprintf(authorizeURL, c.clientID, fullRedirectURL), nil
}
func (c *Client) Authorize(chatID int64, authCode string) (*AuthorizeResponse, error) {
urlParameters := "grant_type=authorization_code&client_id=" + c.clientID + "&client_secret=" + c.clientSecret + "&redirect_uri=" + c.redirectURI + "/?chat_id=" + strconv.Itoa(int(chatID)) + "&code=" + authCode
postData := []byte(urlParameters)
res, reqError := http.Post(authorizeEndpoint, "application/x-www-form-urlencoded", bytes.NewBuffer(postData))
if reqError != nil {
return nil, reqError
}
defer res.Body.Close()
data, readError := io.ReadAll(res.Body)
if readError != nil {
return nil, readError
}
return &AuthorizeResponse{
AccessToken: gjson.Get(string(data), "access_token").String(),
RefreshToken: gjson.Get(string(data), "refresh_token").String(),
}, nil
}
func (c *Client) GetInfoAboutMe(token string) (*MeType, error) {
res, err := c.sendRequest(methodGET, baseURL+meEndpoint, "", token)
if err != nil {
return nil, err
}
var infoAboutMe = &MeType{}
infoAboutMe.Name = gjson.Get(res, "first_name").String()
infoAboutMe.LastName = gjson.Get(res, "last_name").String()
infoAboutMe.MiddleName = gjson.Get(res, "middle_name").String()
infoAboutMe.Email = gjson.Get(res, "email").String()
infoAboutMe.Phone = gjson.Get(res, "phone").String()
infoAboutMe.UserID = gjson.Get(res, "id").String()
return infoAboutMe, nil
}
func (c *Client) Logout(token string) error {
_, reqError := c.sendRequest(methodDELETE, logoutEndpoint, "", token)
if reqError != nil {
return reqError
}
return nil
}