-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecupertoken.go
107 lines (89 loc) · 2.62 KB
/
recupertoken.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
// Package easyapiclient permette un facile utlizzo
// delle API Easyapi di TIM.
package easyapiclient
import (
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
// TokenResponse contiene le risposte di EasyApi.
type TokenResponse struct {
Token string `json:"access_token"`
Scope string `json:"scope"`
Tokentype string `json:"token_type"`
Scadenza int `json:"expires_in"`
}
// RecuperaToken restituisce il token attuale
// e la scadenza dello stesso in sec.
// Se il token è scaduto ne viene generato uno nuovo.
func RecuperaToken(ctx context.Context, username, password string) (token string, scadenza int, err error) {
credenziali := username + ":" + password
authenticator := base64.StdEncoding.EncodeToString([]byte(credenziali))
// Crea variabile per archiviare i risulati.
tokeninfo := new(TokenResponse)
// Accetta anche certificati https non validi.
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// Crea il cliet http.
client := &http.Client{Transport: tr}
// Corpo della chiamata web.
body := strings.NewReader(`grant_type=client_credentials`)
// Crea la request da inviare.
req, err := http.NewRequest("POST",
"https://easyapi.telecomitalia.it:8248/token",
body)
if err != nil {
log.Printf("Errore creazione request: %v\n", req)
return
}
// Aggiunge alla request il contesto.
req.WithContext(ctx)
// Aggiunge alla request l'autenticazione.
req.Header.Set("Authorization",
"Basic "+authenticator)
// Aggiunge alla request gli header per passare le informazioni.
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Invia la request HTTP.
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Errore %s\n", err.Error())
return
}
// Se la http response ha un codice di errore esce.
if resp.StatusCode > 299 {
fmt.Printf("Errore impossibile recuperare token %d\n", resp.StatusCode)
return
}
// Legge il body della risposta.
bodyresp, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf(
"Error Impossibile leggere risposta client http: %s\n",
err.Error())
return
}
// fmt.Println(string(bodyresp))
// Come da specifica chiude il body della response.
defer resp.Body.Close()
// Effettua l'unmashalling dei dati nella variabile.
err = json.Unmarshal(bodyresp, &tokeninfo)
if err != nil {
log.Printf(
"Errore nella scomposizione del json: %s\n",
err.Error())
return
}
/*
fmt.Printf("Token attuale: \t%s\nScadenza tra: \t%d secondi\n",
tokeninfo.Token,
tokeninfo.Scadenza)
*/
return tokeninfo.Token, tokeninfo.Scadenza, err
}