-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_http.go
57 lines (47 loc) · 1.6 KB
/
auth_http.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
package auth
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"github.com/downsized-devs/sdk-go/codes"
"github.com/downsized-devs/sdk-go/errors"
)
const (
ContentType = "Content-Type"
ApplicationJson = "application/json"
ExchangeRefreshTokenURL = "https://securetoken.googleapis.com/v1/token"
)
func (a *auth) exchangeRefreshToken(ctx context.Context, payLoad RefreshTokenRequest) (RefreshTokenResponse, error) {
var result RefreshTokenResponse
bodyPayload, err := a.json.Marshal(payLoad)
if err != nil {
return result, errors.NewWithCode(codes.CodeHttpMarshal, err.Error())
}
var param = url.Values{}
param.Set("key", a.conf.Firebase.ApiKey)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ExchangeRefreshTokenURL+"?"+param.Encode(), bytes.NewBuffer(bodyPayload))
if err != nil {
return result, errors.NewWithCode(codes.CodeErrorHttpNewRequest, err.Error())
}
req.Header.Set(ContentType, ApplicationJson)
resp, err := a.httpClient.Do(req)
if err != nil {
return result, errors.NewWithCode(codes.CodeErrorHttpDo, err.Error())
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return result, errors.NewWithCode(codes.CodeErrorIoutilReadAll, err.Error())
}
if resp.StatusCode != 200 {
a.log.Error(ctx, fmt.Sprintf("error exchange refresh token, req: %v, resp: %s", payLoad, string(body)))
return result, errors.NewWithCode(codes.CodeErrorHttpDo, "error exchangeRefreshToken")
}
err = a.json.Unmarshal(body, &result)
if err != nil {
return result, errors.NewWithCode(codes.CodeUnmarshal, "error exchangeRefreshToken")
}
return result, nil
}