This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathshared.go
115 lines (94 loc) · 2.99 KB
/
shared.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
108
109
110
111
112
113
114
115
/*
Copyright 2019 Padduck, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package oauth2
import (
"bytes"
"encoding/json"
"errors"
"github.com/pufferpanel/apufferi/v4/logging"
"github.com/pufferpanel/pufferd/v2/commons"
"github.com/spf13/viper"
"net/http"
"net/url"
"strconv"
"sync"
"time"
)
var atLocker = &sync.RWMutex{}
var daemonToken string
var lastRefresh time.Time
var expiresIn time.Duration
var client = &http.Client{}
func RefreshToken() bool {
atLocker.Lock()
defer atLocker.Unlock()
//if we just refreshed in the last minute, don't refresh the token
if lastRefresh.Add(1 * time.Minute).After(time.Now()) {
return false
}
clientId := viper.GetString("auth.clientId")
if clientId == "" {
logging.Exception("error talking to auth server", errors.New("client id not specified"))
return false
}
clientSecret := viper.GetString("auth.clientSecret")
if clientSecret == "" {
logging.Exception("error talking to auth server", errors.New("client secret not specified"))
return false
}
data := url.Values{}
data.Set("grant_type", "client_credentials")
data.Set("client_id", clientId)
data.Set("client_secret", clientSecret)
encodedData := data.Encode()
request := createRequest(encodedData)
request.Header.Add("Authorization", "Bearer "+daemonToken)
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
request.Header.Add("Content-Length", strconv.Itoa(len(encodedData)))
response, err := client.Do(request)
defer commons.CloseResponse(response)
if err != nil {
logging.Exception("error talking to auth server", err)
return false
}
var responseData requestResponse
err = json.NewDecoder(response.Body).Decode(&responseData)
if responseData.Error != "" {
return false
}
daemonToken = responseData.AccessToken
lastRefresh = time.Now()
expiresIn = responseData.ExpiresIn
return true
}
func RefreshIfStale() {
//we know the token only lasts about an hour,
//so we'll check to see if we know the cache is old
atLocker.RLock()
oldCache := lastRefresh.Add(expiresIn).Before(time.Now())
atLocker.RUnlock()
if oldCache {
RefreshToken()
}
}
func createRequest(encodedData string) (request *http.Request) {
authUrl := viper.GetString("auth.url")
logging.Debug("Using standard http connection")
request, _ = http.NewRequest("POST", authUrl+"/oauth2/token", bytes.NewBufferString(encodedData))
return
}
type requestResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn time.Duration `json:"expires_in"`
Error string `json:"error"`
}