2019-02-02 05:11:30 -06:00
|
|
|
package authtoken
|
|
|
|
|
|
|
|
import (
|
2019-02-06 09:21:16 -06:00
|
|
|
"encoding/json"
|
2019-02-02 05:11:30 -06:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-02-02 05:11:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2019-02-06 09:45:48 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-02-02 05:11:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserAuthToken(t *testing.T) {
|
|
|
|
Convey("Test user auth token", t, func() {
|
|
|
|
ctx := createTestContext(t)
|
|
|
|
userAuthTokenService := ctx.tokenService
|
|
|
|
userID := int64(10)
|
|
|
|
|
|
|
|
t := time.Date(2018, 12, 13, 13, 45, 0, 0, time.UTC)
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("When creating token", func() {
|
|
|
|
userToken, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
|
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(userToken, ShouldNotBeNil)
|
|
|
|
So(userToken.AuthTokenSeen, ShouldBeFalse)
|
2019-02-02 05:11:30 -06:00
|
|
|
|
|
|
|
Convey("When lookup unhashed token should return user auth token", func() {
|
2019-02-06 09:21:16 -06:00
|
|
|
userToken, err := userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(userToken, ShouldNotBeNil)
|
|
|
|
So(userToken.UserId, ShouldEqual, userID)
|
|
|
|
So(userToken.AuthTokenSeen, ShouldBeTrue)
|
2019-02-02 05:11:30 -06:00
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
storedAuthToken, err := ctx.getAuthTokenByID(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(storedAuthToken, ShouldNotBeNil)
|
|
|
|
So(storedAuthToken.AuthTokenSeen, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When lookup hashed token should return user auth token not found error", func() {
|
2019-02-06 09:21:16 -06:00
|
|
|
userToken, err := userAuthTokenService.LookupToken(userToken.AuthToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldEqual, ErrAuthTokenNotFound)
|
|
|
|
So(userToken, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("revoking existing token should delete token", func() {
|
|
|
|
err = userAuthTokenService.RevokeToken(userToken)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
model, err := ctx.getAuthTokenByID(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(model, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("revoking nil token should return error", func() {
|
|
|
|
err = userAuthTokenService.RevokeToken(nil)
|
|
|
|
So(err, ShouldEqual, ErrAuthTokenNotFound)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("revoking non-existing token should return error", func() {
|
2019-02-06 09:21:16 -06:00
|
|
|
userToken.Id = 1000
|
|
|
|
err = userAuthTokenService.RevokeToken(userToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldEqual, ErrAuthTokenNotFound)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("expires correctly", func() {
|
|
|
|
userToken, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
userToken, err = userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return t.Add(time.Hour)
|
|
|
|
}
|
|
|
|
|
|
|
|
rotated, err := userAuthTokenService.TryRotateToken(userToken, "192.168.10.11:1234", "some user agent")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(rotated, ShouldBeTrue)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
userToken, err = userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
stillGood, err := userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(stillGood, ShouldNotBeNil)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
model, err := ctx.getAuthTokenByID(userToken.Id)
|
2019-02-06 01:30:14 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("when rotated_at is 6:59:59 ago should find token", func() {
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return time.Unix(model.RotatedAt, 0).Add(24 * 7 * time.Hour).Add(-time.Second)
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
stillGood, err = userAuthTokenService.LookupToken(stillGood.UnhashedToken)
|
2019-02-06 01:30:14 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(stillGood, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("when rotated_at is 7:00:00 ago should not find token", func() {
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return time.Unix(model.RotatedAt, 0).Add(24 * 7 * time.Hour)
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
notGood, err := userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-06 01:30:14 -06:00
|
|
|
So(err, ShouldEqual, ErrAuthTokenNotFound)
|
|
|
|
So(notGood, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("when rotated_at is 5 days ago and created_at is 29 days and 23:59:59 ago should not find token", func() {
|
|
|
|
updated, err := ctx.updateRotatedAt(model.Id, time.Unix(model.CreatedAt, 0).Add(24*25*time.Hour).Unix())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(updated, ShouldBeTrue)
|
|
|
|
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return time.Unix(model.CreatedAt, 0).Add(24 * 30 * time.Hour).Add(-time.Second)
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
stillGood, err = userAuthTokenService.LookupToken(stillGood.UnhashedToken)
|
2019-02-06 01:30:14 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(stillGood, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("when rotated_at is 5 days ago and created_at is 30 days ago should not find token", func() {
|
|
|
|
updated, err := ctx.updateRotatedAt(model.Id, time.Unix(model.CreatedAt, 0).Add(24*25*time.Hour).Unix())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(updated, ShouldBeTrue)
|
|
|
|
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return time.Unix(model.CreatedAt, 0).Add(24 * 30 * time.Hour)
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
notGood, err := userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-06 01:30:14 -06:00
|
|
|
So(err, ShouldEqual, ErrAuthTokenNotFound)
|
|
|
|
So(notGood, ShouldBeNil)
|
|
|
|
})
|
2019-02-02 05:11:30 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("can properly rotate tokens", func() {
|
|
|
|
userToken, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
prevToken := userToken.AuthToken
|
|
|
|
unhashedPrev := userToken.UnhashedToken
|
2019-02-02 05:11:30 -06:00
|
|
|
|
|
|
|
rotated, err := userAuthTokenService.TryRotateToken(userToken, "192.168.10.12:1234", "a new user agent")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(rotated, ShouldBeFalse)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
updated, err := ctx.markAuthTokenAsSeen(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(updated, ShouldBeTrue)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
model, err := ctx.getAuthTokenByID(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2019-02-06 09:45:48 -06:00
|
|
|
var tok models.UserToken
|
2019-02-06 09:21:16 -06:00
|
|
|
model.toUserToken(&tok)
|
|
|
|
|
2019-02-02 05:11:30 -06:00
|
|
|
getTime = func() time.Time {
|
|
|
|
return t.Add(time.Hour)
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
rotated, err = userAuthTokenService.TryRotateToken(&tok, "192.168.10.12:1234", "a new user agent")
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(rotated, ShouldBeTrue)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
unhashedToken := tok.UnhashedToken
|
2019-02-02 05:11:30 -06:00
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
model, err = ctx.getAuthTokenByID(tok.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
model.UnhashedToken = unhashedToken
|
|
|
|
|
|
|
|
So(model.RotatedAt, ShouldEqual, getTime().Unix())
|
|
|
|
So(model.ClientIp, ShouldEqual, "192.168.10.12")
|
|
|
|
So(model.UserAgent, ShouldEqual, "a new user agent")
|
|
|
|
So(model.AuthTokenSeen, ShouldBeFalse)
|
|
|
|
So(model.SeenAt, ShouldEqual, 0)
|
|
|
|
So(model.PrevAuthToken, ShouldEqual, prevToken)
|
|
|
|
|
|
|
|
// ability to auth using an old token
|
|
|
|
|
|
|
|
lookedUpUserToken, err := userAuthTokenService.LookupToken(model.UnhashedToken)
|
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(lookedUpUserToken, ShouldNotBeNil)
|
|
|
|
So(lookedUpUserToken.AuthTokenSeen, ShouldBeTrue)
|
|
|
|
So(lookedUpUserToken.SeenAt, ShouldEqual, getTime().Unix())
|
2019-02-02 05:11:30 -06:00
|
|
|
|
|
|
|
lookedUpUserToken, err = userAuthTokenService.LookupToken(unhashedPrev)
|
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(lookedUpUserToken, ShouldNotBeNil)
|
|
|
|
So(lookedUpUserToken.Id, ShouldEqual, model.Id)
|
|
|
|
So(lookedUpUserToken.AuthTokenSeen, ShouldBeTrue)
|
2019-02-02 05:11:30 -06:00
|
|
|
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return t.Add(time.Hour + (2 * time.Minute))
|
|
|
|
}
|
|
|
|
|
|
|
|
lookedUpUserToken, err = userAuthTokenService.LookupToken(unhashedPrev)
|
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(lookedUpUserToken, ShouldNotBeNil)
|
|
|
|
So(lookedUpUserToken.AuthTokenSeen, ShouldBeTrue)
|
2019-02-02 05:11:30 -06:00
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
lookedUpModel, err := ctx.getAuthTokenByID(lookedUpUserToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(lookedUpModel, ShouldNotBeNil)
|
|
|
|
So(lookedUpModel.AuthTokenSeen, ShouldBeFalse)
|
|
|
|
|
|
|
|
rotated, err = userAuthTokenService.TryRotateToken(userToken, "192.168.10.12:1234", "a new user agent")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(rotated, ShouldBeTrue)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
model, err = ctx.getAuthTokenByID(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(model, ShouldNotBeNil)
|
|
|
|
So(model.SeenAt, ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("keeps prev token valid for 1 minute after it is confirmed", func() {
|
|
|
|
userToken, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
|
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(userToken, ShouldNotBeNil)
|
2019-02-02 05:11:30 -06:00
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
lookedUpUserToken, err := userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(lookedUpUserToken, ShouldNotBeNil)
|
|
|
|
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return t.Add(10 * time.Minute)
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
prevToken := userToken.UnhashedToken
|
2019-02-02 05:11:30 -06:00
|
|
|
rotated, err := userAuthTokenService.TryRotateToken(userToken, "1.1.1.1", "firefox")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(rotated, ShouldBeTrue)
|
|
|
|
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return t.Add(20 * time.Minute)
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
currentUserToken, err := userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(currentUserToken, ShouldNotBeNil)
|
|
|
|
|
|
|
|
prevUserToken, err := userAuthTokenService.LookupToken(prevToken)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(prevUserToken, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("will not mark token unseen when prev and current are the same", func() {
|
|
|
|
userToken, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
|
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(userToken, ShouldNotBeNil)
|
2019-02-02 05:11:30 -06:00
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
lookedUpUserToken, err := userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(lookedUpUserToken, ShouldNotBeNil)
|
2019-02-02 05:11:30 -06:00
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
lookedUpUserToken, err = userAuthTokenService.LookupToken(userToken.UnhashedToken)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(lookedUpUserToken, ShouldNotBeNil)
|
2019-02-02 05:11:30 -06:00
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
lookedUpModel, err := ctx.getAuthTokenByID(lookedUpUserToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(lookedUpModel, ShouldNotBeNil)
|
|
|
|
So(lookedUpModel.AuthTokenSeen, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Rotate token", func() {
|
|
|
|
userToken, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
|
|
|
|
So(err, ShouldBeNil)
|
2019-02-06 09:21:16 -06:00
|
|
|
So(userToken, ShouldNotBeNil)
|
2019-02-02 05:11:30 -06:00
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
prevToken := userToken.AuthToken
|
2019-02-02 05:11:30 -06:00
|
|
|
|
|
|
|
Convey("Should rotate current token and previous token when auth token seen", func() {
|
2019-02-06 09:21:16 -06:00
|
|
|
updated, err := ctx.markAuthTokenAsSeen(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(updated, ShouldBeTrue)
|
|
|
|
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return t.Add(10 * time.Minute)
|
|
|
|
}
|
|
|
|
|
|
|
|
rotated, err := userAuthTokenService.TryRotateToken(userToken, "1.1.1.1", "firefox")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(rotated, ShouldBeTrue)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
storedToken, err := ctx.getAuthTokenByID(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(storedToken, ShouldNotBeNil)
|
|
|
|
So(storedToken.AuthTokenSeen, ShouldBeFalse)
|
|
|
|
So(storedToken.PrevAuthToken, ShouldEqual, prevToken)
|
|
|
|
So(storedToken.AuthToken, ShouldNotEqual, prevToken)
|
|
|
|
|
|
|
|
prevToken = storedToken.AuthToken
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
updated, err = ctx.markAuthTokenAsSeen(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(updated, ShouldBeTrue)
|
|
|
|
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return t.Add(20 * time.Minute)
|
|
|
|
}
|
|
|
|
|
|
|
|
rotated, err = userAuthTokenService.TryRotateToken(userToken, "1.1.1.1", "firefox")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(rotated, ShouldBeTrue)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
storedToken, err = ctx.getAuthTokenByID(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(storedToken, ShouldNotBeNil)
|
|
|
|
So(storedToken.AuthTokenSeen, ShouldBeFalse)
|
|
|
|
So(storedToken.PrevAuthToken, ShouldEqual, prevToken)
|
|
|
|
So(storedToken.AuthToken, ShouldNotEqual, prevToken)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should rotate current token, but keep previous token when auth token not seen", func() {
|
2019-02-06 09:21:16 -06:00
|
|
|
userToken.RotatedAt = getTime().Add(-2 * time.Minute).Unix()
|
2019-02-02 05:11:30 -06:00
|
|
|
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return t.Add(2 * time.Minute)
|
|
|
|
}
|
|
|
|
|
|
|
|
rotated, err := userAuthTokenService.TryRotateToken(userToken, "1.1.1.1", "firefox")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(rotated, ShouldBeTrue)
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
storedToken, err := ctx.getAuthTokenByID(userToken.Id)
|
2019-02-02 05:11:30 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(storedToken, ShouldNotBeNil)
|
|
|
|
So(storedToken.AuthTokenSeen, ShouldBeFalse)
|
|
|
|
So(storedToken.PrevAuthToken, ShouldEqual, prevToken)
|
|
|
|
So(storedToken.AuthToken, ShouldNotEqual, prevToken)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-02-06 09:21:16 -06:00
|
|
|
Convey("When populating userAuthToken from UserToken should copy all properties", func() {
|
2019-02-06 09:45:48 -06:00
|
|
|
ut := models.UserToken{
|
2019-02-06 09:21:16 -06:00
|
|
|
Id: 1,
|
|
|
|
UserId: 2,
|
|
|
|
AuthToken: "a",
|
|
|
|
PrevAuthToken: "b",
|
|
|
|
UserAgent: "c",
|
|
|
|
ClientIp: "d",
|
|
|
|
AuthTokenSeen: true,
|
|
|
|
SeenAt: 3,
|
|
|
|
RotatedAt: 4,
|
|
|
|
CreatedAt: 5,
|
|
|
|
UpdatedAt: 6,
|
|
|
|
UnhashedToken: "e",
|
|
|
|
}
|
|
|
|
utBytes, err := json.Marshal(ut)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
utJSON, err := simplejson.NewJson(utBytes)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
utMap := utJSON.MustMap()
|
|
|
|
|
|
|
|
var uat userAuthToken
|
|
|
|
uat.fromUserToken(&ut)
|
|
|
|
uatBytes, err := json.Marshal(uat)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
uatJSON, err := simplejson.NewJson(uatBytes)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
uatMap := uatJSON.MustMap()
|
|
|
|
|
|
|
|
So(uatMap, ShouldResemble, utMap)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When populating userToken from userAuthToken should copy all properties", func() {
|
|
|
|
uat := userAuthToken{
|
|
|
|
Id: 1,
|
|
|
|
UserId: 2,
|
|
|
|
AuthToken: "a",
|
|
|
|
PrevAuthToken: "b",
|
|
|
|
UserAgent: "c",
|
|
|
|
ClientIp: "d",
|
|
|
|
AuthTokenSeen: true,
|
|
|
|
SeenAt: 3,
|
|
|
|
RotatedAt: 4,
|
|
|
|
CreatedAt: 5,
|
|
|
|
UpdatedAt: 6,
|
|
|
|
UnhashedToken: "e",
|
|
|
|
}
|
|
|
|
uatBytes, err := json.Marshal(uat)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
uatJSON, err := simplejson.NewJson(uatBytes)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
uatMap := uatJSON.MustMap()
|
|
|
|
|
2019-02-06 09:45:48 -06:00
|
|
|
var ut models.UserToken
|
2019-02-06 09:21:16 -06:00
|
|
|
err = uat.toUserToken(&ut)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
utBytes, err := json.Marshal(ut)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
utJSON, err := simplejson.NewJson(utBytes)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
utMap := utJSON.MustMap()
|
|
|
|
|
|
|
|
So(utMap, ShouldResemble, uatMap)
|
|
|
|
})
|
|
|
|
|
2019-02-02 05:11:30 -06:00
|
|
|
Reset(func() {
|
|
|
|
getTime = time.Now
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestContext(t *testing.T) *testContext {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
sqlstore := sqlstore.InitTestDB(t)
|
|
|
|
tokenService := &UserAuthTokenServiceImpl{
|
|
|
|
SQLStore: sqlstore,
|
|
|
|
Cfg: &setting.Cfg{
|
2019-02-05 14:12:30 -06:00
|
|
|
LoginMaxInactiveLifetimeDays: 7,
|
|
|
|
LoginMaxLifetimeDays: 30,
|
|
|
|
TokenRotationIntervalMinutes: 10,
|
|
|
|
ExpiredTokensCleanupIntervalDays: 1,
|
2019-02-02 05:11:30 -06:00
|
|
|
},
|
|
|
|
log: log.New("test-logger"),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &testContext{
|
|
|
|
sqlstore: sqlstore,
|
|
|
|
tokenService: tokenService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testContext struct {
|
|
|
|
sqlstore *sqlstore.SqlStore
|
|
|
|
tokenService *UserAuthTokenServiceImpl
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testContext) getAuthTokenByID(id int64) (*userAuthToken, error) {
|
|
|
|
sess := c.sqlstore.NewSession()
|
|
|
|
var t userAuthToken
|
|
|
|
found, err := sess.ID(id).Get(&t)
|
|
|
|
if err != nil || !found {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testContext) markAuthTokenAsSeen(id int64) (bool, error) {
|
|
|
|
sess := c.sqlstore.NewSession()
|
|
|
|
res, err := sess.Exec("UPDATE user_auth_token SET auth_token_seen = ? WHERE id = ?", c.sqlstore.Dialect.BooleanStr(true), id)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return rowsAffected == 1, nil
|
|
|
|
}
|
2019-02-06 01:30:14 -06:00
|
|
|
|
|
|
|
func (c *testContext) updateRotatedAt(id, rotatedAt int64) (bool, error) {
|
|
|
|
sess := c.sqlstore.NewSession()
|
|
|
|
res, err := sess.Exec("UPDATE user_auth_token SET rotated_at = ? WHERE id = ?", rotatedAt, id)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return rowsAffected == 1, nil
|
|
|
|
}
|