-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
59 lines (48 loc) · 1.48 KB
/
token.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
package o365Api
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
type Token interface {
GetUserBearerToken() (TokenResponse, error)
}
type TokenRequest struct {
Client_ID string
Client_Secret string
Tenant_ID string
UserName string
UserPassword string
}
type TokenResponse struct {
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
ExtExpiresIn int `json:"ext_expires_in"`
AccessToken string `json:"access_token"`
}
func (t TokenRequest) GetUserBearerToken() (TokenResponse, error) {
if len(t.Client_ID) == 0 || len(t.Client_Secret) == 0 || len(t.Tenant_ID) == 0 || len(t.UserName) == 0 || len(t.UserPassword) == 0 {
return TokenResponse{}, errors.New("TokenRequest is not valid")
}
url := fmt.Sprintf("https://login.microsoftonline.com/%s/oauth2/v2.0/token", t.Tenant_ID)
payload := strings.NewReader(fmt.Sprintf("grant_type=password&client_id=%s&client_secret=%s&scope=https://graph.microsoft.com/.default&userName=%s&password=%s",
t.Client_ID, t.Client_Secret, t.UserName, t.UserPassword))
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("cache-control", "no-cache")
res, err := http.DefaultClient.Do(req)
if err != nil {
return TokenResponse{}, err
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var resp TokenResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return TokenResponse{}, err
}
return resp, nil
}