Skip to content

Commit ac8a26b

Browse files
author
Karl Wolffgang
committed
Make new release
1 parent b564fc0 commit ac8a26b

File tree

3 files changed

+62
-35
lines changed

3 files changed

+62
-35
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4-
## [Unreleased]
4+
## [1.2]
5+
### Added
6+
7+
- Refresh the access token automaticaly
8+
59
### Changed
610

711
- the structur of the userinfo

src/main.go

+11-28
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ type void struct{}
1616

1717
type userState struct {
1818
username string
19-
accessToken string
20-
refreshToken string
21-
expiry time.Time
2219
superuser bool
2320
readTopics []string
2421
writeTopics []string
@@ -27,6 +24,7 @@ type userState struct {
2724
updatedAt time.Time
2825
usernameIsToken bool
2926
client *http.Client
27+
token *oauth2.Token
3028
}
3129

3230
// type Topics struct {
@@ -122,17 +120,6 @@ func cacheIsValid(cache *userState) bool {
122120
return false
123121
}
124122

125-
func tokenNotExpired(expiredAt time.Time) bool {
126-
log.Debugf("Token should expired at: %s", expiredAt.Format(time.RFC3339))
127-
128-
if (time.Now().Sub(expiredAt)) < 0 {
129-
log.Debug("Token is still valid.")
130-
return true
131-
}
132-
133-
return false
134-
}
135-
136123
func createUserWithCredentials(username, password string) bool {
137124
token, err := config.PasswordCredentialsToken(context.Background(), username, password)
138125

@@ -145,43 +132,39 @@ func createUserWithCredentials(username, password string) bool {
145132

146133
userCache[username] = userState{
147134
username: username,
148-
accessToken: token.AccessToken,
149-
refreshToken: token.RefreshToken,
150-
expiry: token.Expiry,
151135
superuser: false,
152136
createdAt: time.Now(),
153137
updatedAt: time.Unix(0, 0),
154138
client: oauthClient,
139+
token: token,
155140
}
156141

157142
return true
158143
}
159144

160-
func createUserWithToken(token string) bool {
161-
tokenSet := &oauth2.Token{
162-
AccessToken: token,
145+
func createUserWithToken(accessToken string) bool {
146+
token := &oauth2.Token{
147+
AccessToken: accessToken,
163148
TokenType: "Bearer",
164149
}
165-
client := config.Client(context.Background(), tokenSet)
150+
client := config.Client(context.Background(), token)
166151
info, err := getUserInfo(client)
167152

168153
if err != nil {
169154
log.Println(err)
170155
return false
171156
}
172157

173-
userCache[token] = userState{
174-
username: token,
175-
accessToken: token,
158+
userCache[accessToken] = userState{
159+
username: accessToken,
176160
usernameIsToken: true,
177-
refreshToken: "",
178-
expiry: time.Unix(0, 0),
179161
superuser: info.MQTT.Superuser,
180162
createdAt: time.Now(),
181163
updatedAt: time.Now(),
182164
readTopics: info.MQTT.Topics.Read,
183165
writeTopics: info.MQTT.Topics.Write,
184166
client: client,
167+
token: token,
185168
}
186169

187170
return true
@@ -261,8 +244,8 @@ func GetSuperuser(username string) bool {
261244
}
262245

263246
if !cacheIsValid(&cache) {
264-
if !tokenNotExpired(cache.expiry) {
265-
log.Warningf("Token for user %s expired. Try to refresh.", username)
247+
if !cache.token.Valid() {
248+
log.Warningf("Token for user %s invalid. Try to refresh.", username)
266249
}
267250

268251
info, err := getUserInfo(cache.client)

src/main_test.go

+46-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import (
55
"net/http/httptest"
66
"strconv"
77
"testing"
8+
"time"
9+
"os"
810

911
log "github.com/sirupsen/logrus"
1012
)
1113

14+
// init environmental infos
15+
var CI bool
16+
1217
func setupMockOAuthServer() (*httptest.Server, func()) {
1318
mux := http.NewServeMux()
1419
mux.HandleFunc("/userinfo", func(w http.ResponseWriter, r *http.Request) {
@@ -92,6 +97,9 @@ func createOAuthServer(t *testing.T, duration int) (*httptest.Server, func()) {
9297
}
9398

9499
func TestInit(t *testing.T) {
100+
CI = (os.Getenv("DRONE") == "true")
101+
log.Infof("Run the test in the ci: %t", CI)
102+
95103
_, closeServer := createOAuthServer(t, 0)
96104
defer closeServer()
97105
}
@@ -233,16 +241,48 @@ func TestGetUserinfoFromCache(t *testing.T) {
233241
}
234242
}
235243

236-
func TestTokenExpired(t *testing.T) {
244+
func TestRefreshExpiredAccessTokenCredentials(t *testing.T) {
245+
// hard to test. when set the expired_at in the response to a
246+
// short time, the client call the refresh request instant
247+
// but nobody like long tests so the test only runs on ci
248+
if CI {
249+
// first init plugin to create oauth server and client
250+
_, closeServer := createOAuthServer(t, 0)
251+
defer closeServer()
252+
253+
GetUser("test_superuser", "test_superuser")
254+
237255

256+
time.Sleep(65 * time.Second)
257+
258+
// second try after expired
259+
allowed := GetSuperuser("test_superuser")
260+
if !allowed {
261+
t.Errorf("Test cache check was positive")
262+
}
263+
}
238264
}
239265

240-
func TestRefreshExpiredAccessToken(t *testing.T) {
241-
// first init plugin to create oauth server and client
242-
_, closeServer := createOAuthServer(t, 10)
243-
defer closeServer()
266+
func TestRefreshExpiredAccessTokenWithoutCrediantials(t *testing.T) {
267+
// hard to test. when set the expired_at in the response to a
268+
// short time, the client call the refresh request instant
269+
// but nobody like long tests so the test only runs on ci
270+
if CI {
271+
// first init plugin to create oauth server and client
272+
_, closeServer := createOAuthServer(t, 0)
273+
defer closeServer()
274+
275+
GetUser("mock_token_superuser", "")
244276

245-
GetUser("test_superuser", "test_superuser")
246277

278+
time.Sleep(65 * time.Second)
247279

280+
// second try after expired
281+
allowed := GetSuperuser("mock_token_superuser")
282+
if !allowed {
283+
t.Errorf("Test cache check was positive")
284+
}
285+
}
248286
}
287+
288+

0 commit comments

Comments
 (0)