This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
125 lines (111 loc) · 2.7 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
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
package gobizApi
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type GojekAuth struct {
deviceOS string
phoneMake string
phoneModel string
uniqueID string
}
type OtpResponse struct {
Data struct {
OtpToken string `json:"otp_token"`
} `json:"data"`
Success bool `json:"success"`
Errors []string `json:"errors"`
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
}
func NewGojekAuth(deviceOS, phoneMake, phoneModel, uniqueID string) *GojekAuth {
return &GojekAuth{
deviceOS: deviceOS,
phoneMake: phoneMake,
phoneModel: phoneModel,
uniqueID: uniqueID,
}
}
func (g *GojekAuth) GetUniqueID() string {
return g.uniqueID
}
func (g *GojekAuth) SetUniqueID(uniqueID string) {
g.uniqueID = uniqueID
}
func (g *GojekAuth) setHeaders(req *http.Request) {
req.Header.Set("X-Uniqueid", g.uniqueID)
req.Header.Set("X-Deviceos", g.deviceOS)
req.Header.Set("X-Phonemake", g.phoneMake)
req.Header.Set("X-Phonemodel", g.phoneModel)
}
func (g *GojekAuth) GetOtpToken(number int) (string, error) {
data := map[string]string{
"phone_number": fmt.Sprintf("%d", number),
"client_secret": "sPC0qVk7gi76JUoGVfOfcgd7FfuaBv",
"country_code": "+62",
"client_id": "go-biz-mobile",
}
jsonValue, err := json.Marshal(data)
if err != nil {
printError(err)
return "", err
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://goid.gojekapi.com/goid/login/request"), bytes.NewBuffer(jsonValue))
if err != nil {
printError(err)
return "", err
}
g.setHeaders(req)
body, err := sendRequestGuest(req)
if err != nil {
printResponse(string(body))
printError(err)
return "", err
}
var otpResponse OtpResponse
err = json.Unmarshal(body, &otpResponse)
if err != nil {
printError(err)
return "", err
}
printResponse(string(body))
return otpResponse.Data.OtpToken, nil
}
func (g *GojekAuth) ProcessOtpToken(otp int, otptoken string) (string, error) {
data := map[string]interface{}{
"client_secret": "sPC0qVk7gi76JUoGVfOfcgd7FfuaBv",
"grant_type": "otp",
"data": map[string]string{
"otp_token": otptoken,
"otp": fmt.Sprintf("%d", otp),
},
"client_id": "go-biz-mobile",
}
jsonValue, err := json.Marshal(data)
if err != nil {
printError(err)
return "", err
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://goid.gojekapi.com/goid/token"), bytes.NewBuffer(jsonValue))
if err != nil {
printError(err)
return "", err
}
g.setHeaders(req)
body, err := sendRequestGuest(req)
if err != nil {
printError(err)
return "", err
}
var tokenResponse TokenResponse
err = json.Unmarshal(body, &tokenResponse)
if err != nil {
printError(err)
return "", err
}
printResponse(string(body))
return tokenResponse.AccessToken, nil
}