-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
165 lines (141 loc) · 4.83 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"encoding/json"
"errors"
"time"
"fmt"
"log"
"os"
"crypto/tls"
"github.com/parnurzeal/gorequest"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
type Credential struct {
Email string `json:email`
Password string `json:password`
Token string
ClientID string
}
func getServer() *gin.Engine {
server := gin.Default()
server.Use(cors.New(cors.Config{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST"},
AllowHeaders: []string{"Location", "Accept", "Authorization", "Content-Type"},
ExposeHeaders: []string{"Link", "Location"},
MaxAge: 1 * time.Hour,
}))
return server
}
func authenticateBy(credential Credential) (error, Credential) {
url := "http://api.pontomaisweb.com.br/api/auth/sign_in"
requestData := fmt.Sprintf(`{
"email": "%s",
"password": "%s"}`,
credential.Email, credential.Password)
request := gorequest.New()
response, body, errs := request.Post(url).
Set("User-Agent", "Mozilla/5.0 (Linux; Android 6.0.1; MotoG3 Build/MOB31K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/51.0.2704.106 Mobile Safari/537.36").
Set("Content-Type", "application/json").
Set("X-Requested-With", "br.com.pontomais.pontomais").
Send(requestData).
End()
var err error
if len(errs) > 0 {
log.Println("[ERROR] Falhou autenticando na api do pontomais.")
return err, credential
}
var result map[string]interface{}
err = json.Unmarshal([]byte(body), &result)
if response.StatusCode > 201 || err != nil {
msg := fmt.Sprintf("HttpStatus: %d. Email: %s. error: %s", response.StatusCode, credential.Email, err)
err = errors.New(msg)
return err, credential
}
credential.Token = result["token"].(string)
credential.ClientID = result["client_id"].(string)
return err, credential
}
func registerTimeclockBy(credential Credential) (error, time.Time) {
url := "https://api.pontomaisweb.com.br/api/time_cards/register"
requestData := fmt.Sprintf(`{
"_path": "/meu_ponto/registro_de_ponto",
"time_card": {
"accuracy": 600,
"accuracy_method": true,
"address": "Av. das Nações Unidas, 11541 - Cidade Monções, São Paulo - SP, Brasil",
"latitude": -23.6015797,
"location_edited": false,
"longitude": -46.694767,
"original_address": "Av. das Nações Unidas, 11541 - Cidade Monções, São Paulo - SP, Brasil",
"original_latitude": -23.6015797,
"original_longitude": -46.694767,
"reference_id": null
}}`)
request := gorequest.New()
response, body, errs:= request.Post(url).
Set("User-Agent", "Mozilla/5.0 (Linux; Android 6.0.1; MotoG3 Build/MOB31K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/51.0.2704.106 Mobile Safari/537.36").
Set("Content-Type", "application/json").
Set("X-Requested-With", "br.com.pontomais.pontomais").
Set("token-type", "Bearer").
Set("uid", credential.Email).
Set("access-token", credential.Token).
Set("client", credential.ClientID).
TLSClientConfig(&tls.Config{ InsecureSkipVerify: true}).
Send(requestData).
End()
var err error
if len(errs) > 0 {
log.Println("[ERROR] Falhou ao registrar ponto na api do pontomais.", errs[0])
return err, time.Now()
}
var result map[string]interface{}
err = json.Unmarshal([]byte(body), &result)
if response.StatusCode > 201 || err != nil {
msg := fmt.Sprintf("HttpStatus: %d. Email: %s. error: %s", response.StatusCode, credential.Email, err)
err = errors.New(msg)
return err, time.Now()
}
timecard := result["untreated_time_card"].(map[string]interface{})
created_at := timecard["created_at"].(string)
log.Println(created_at + ":" + credential.Email + ":" + credential.Password)
var t time.Time
t, err = time.Parse(time.RFC3339Nano, created_at)
return err, t
}
func makeResponse(message string) interface{} {
return map[string]string{"message": message}
}
func RegisterTimeclock(ctx *gin.Context) {
credential := Credential{}
ctx.BindJSON(&credential)
var err error
err, credential = authenticateBy(credential)
status := 200
message := "Success"
if err != nil {
status = 403
message = fmt.Sprintf("Autenticacao falhou: %s", err)
log.Println("[ERROR]", message)
}
err, t := registerTimeclockBy(credential)
if err != nil {
status = 500
message = fmt.Sprintf("Falhou ao registrar o ponto. email: %s. token: %s. client_id: %s. error: %s", credential.Email, credential.Token, credential.ClientID, err)
log.Println("[ERROR]", message)
}
message = fmt.Sprintf("%s", t.Format("20060102150405"))
ctx.JSON(status, makeResponse(message))
}
func main() {
gin.SetMode(gin.ReleaseMode)
server := getServer()
server.POST("/", RegisterTimeclock)
port := os.Getenv("PORT")
if len(port) == 0 {
port = "7000"
}
log.Println("Listening on", port)
server.Run(":"+port)
}