-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (54 loc) · 1.36 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
package main
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
type LoginCredentials struct {
Username string `json:"username"`
Password string `json:"password"`
}
func encodeCredentials(credentials LoginCredentials) string {
credStr := fmt.Sprintf("%s:%s", credentials.Username, credentials.Password)
return base64.StdEncoding.EncodeToString([]byte(credStr))
}
func signin(credentials LoginCredentials) (string, error) {
client := &http.Client{}
data := bytes.NewBuffer([]byte{})
req, err := http.NewRequest("POST", "https://zone01normandie.org/api/auth/signin", data)
if err != nil {
return "", err
}
//encodedCredentials := encodeCredentials(credentials)
req.Header.Set("Authorization", "Basic "+"bGFoYmlibWFsZWtAaG90bWFpbC5jb206TWFsaWtvdi04MA==")
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return "", errors.New(string(body))
}
token, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(token), nil
}
func main() {
// Replace with your credentials
credentials := LoginCredentials{
Username: "test ",
Password: "test",
}
jwt, err := signin(credentials)
if err != nil {
fmt.Println("Error logging in:", err)
return
}
fmt.Println("JWT obtained:", jwt)
}