-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
113 lines (90 loc) · 2.58 KB
/
utils.go
File metadata and controls
113 lines (90 loc) · 2.58 KB
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
108
109
110
111
112
113
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
type TokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
func LoadEnvVariables() {
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Error loading .env file")
os.Exit(1)
}
clientId = os.Getenv("CLIENT_ID")
clientSecret = os.Getenv("CLIENT_SECRET")
redirectUri = os.Getenv("REDIRECT_URI")
scopes = os.Getenv("SCOPES")
accessToken = os.Getenv("ACCESS_TOKEN")
refreshToken = os.Getenv("REFRESH_TOKEN")
authUrl = os.Getenv("AUTH_URL")
tokenUrl = os.Getenv("TOKEN_URL")
tenantId = os.Getenv("TENANT_ID")
fileCount = 20
}
func UpdateEnvFileTokens(newAccessToken string, newRefreshToken string) {
envMap := make(map[string]string)
envMap["CLIENT_ID"] = clientId
envMap["CLIENT_SECRET"] = clientSecret
envMap["REDIRECT_URI"] = redirectUri
envMap["SCOPES"] = scopes
envMap["TENANT_ID"] = tenantId
envMap["ACCESS_TOKEN"] = newAccessToken
envMap["REFRESH_TOKEN"] = newRefreshToken
envMap["AUTH_URL"] = authUrl
envMap["TOKEN_URL"] = tokenUrl
err := godotenv.Write(envMap, ".env")
if err != nil {
log.Fatal("Error writing .env file")
}
os.Setenv("ACCESS_TOKEN", newAccessToken)
os.Setenv("REFRESH_TOKEN", newRefreshToken)
}
func UpdateToken() {
url := "https://login.microsoftonline.com/common/oauth2/v2.0/token"
data := []byte(fmt.Sprintf("grant_type=refresh_token&client_id=%s&client_secret=%s&refresh_token=%s&scope=%s", clientId, clientSecret, refreshToken, scopes))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var token TokenResponse
err = json.Unmarshal(body, &token)
if err != nil {
panic(err)
}
errAccesToken := os.Setenv("ACCESS_TOKEN", token.AccessToken)
if err != nil {
panic(errAccesToken)
}
errRefreshToken := os.Setenv("REFRESH_TOKEN", token.RefreshToken)
if err != nil {
panic(errRefreshToken)
}
UpdateEnvFileTokens(token.AccessToken, token.RefreshToken)
accessToken = token.AccessToken
refreshToken = token.RefreshToken
if accessToken != "" && refreshToken != "" {
fmt.Println("Token updated")
return
}
fmt.Println("Token not updated")
}