-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_jwt_s3zip_token2.go
71 lines (64 loc) · 1.48 KB
/
api_jwt_s3zip_token2.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
package main
import (
"github.com/labstack/echo"
"net/http"
"net/url"
"io/ioutil"
"encoding/json"
"log"
"strings"
"time"
)
func getJWTToken2(c echo.Context) (err error) {
// initiate the client
client := &http.Client{}
// set the form
form := url.Values{}
form.Add("userKey", "user-key")
form.Add("userSecret", "user-secret")
// create a new request
req2 , err2:= http.NewRequest("POST", "https://api.s3zipper.com/gentoken", strings.NewReader(form.Encode()))
if err2 != nil {
log.Fatal("NewRequest: ", err2)
return err2
}
// set form in request
req2.PostForm = form
req2.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// get response
resp3 ,err3 := client.Do(req2)
if err3 != nil{
log.Fatal("NewRequest: ", err3)
return err3
}
defer resp3.Body.Close()
// read the body
body, err4:= ioutil.ReadAll(resp3.Body)
if err4 != nil{
return err4
}
/*******************************************************************/
// Get the token from the body
var p GetToken
err = json.Unmarshal(body, &p)
if err != nil {
log.Fatal("NewRequest: ", err)
return err
}
token := p.Token
//save token in cookie
if len(token) > 1{
//save token
cookie := new(http.Cookie)
cookie.Name = "newJwtToken"
cookie.Value = token
cookie.Path = "/"
cookie.Expires = time.Now().Add(24 * time.Hour)
c.SetCookie(cookie)
}else{
return c.JSON(http.StatusOK, "No token was saved in cookie")
}
return c.JSON(http.StatusOK, map[string]string{
"token": token,
})
}