-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
44 lines (37 loc) · 1.06 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
// OAuth handler
package auth
import(
"fmt"
"gopkg.in/resty.v1"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
)
const (
MixinOauthURL = "https://api.mixin.one/oauth/token"
)
func NewAuthWorker(clientID, appSecret string){
r := gin.Default()
r.GET("/oauth", oauthHandler(clientID, appSecret))
r.Run()
}
func oauthHandler(clientID, appSecret string) gin.HandlerFunc{
fn := func(c *gin.Context){
code := c.Query("code")
if len(code) == 64{
client := resty.New()
body := fmt.Sprintf(`{"client_id":"%s","code":"%s","client_secret":"%s"}`, clientID, code, appSecret)
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(MixinOauthURL)
if err != nil{
fmt.Println(err)
}
accessToken := gjson.Get(resp.String(), `data.access_token`).String()
c.IndentedJSON(200, gin.H{
"token": accessToken,
})
}
}
return fn
}