Example #1
0
func TestRedisStorage(t *testing.T) {

	jwtToken, err := jwt.GenerateJwtToken(testUser)
	if err != nil {
		t.Error(err)
	}
	tkn, _ := NewToken(testUser, jwtToken)

	storage := NewRedisStorage()
	err = storage.OpenSession()
	if err != nil {
		t.Error(err)
	}

	err = storage.InsertToken(tkn)
	if err != nil {
		t.Error(err)
		return
	}

	loadedTkn, err := storage.TokenByRefToken(tkn.RefToken)
	if err != nil {
		t.Error(err)
		return
	}

	if loadedTkn.JwtToken != jwtToken {
		t.Error("Tokens not equal")
	}

}
Example #2
0
func TestNewToken(t *testing.T) {
	jwtToken, err := jwt.GenerateJwtToken(testUser)
	if err != nil {
		t.Error(err)
	}
	token, _ := NewToken(testUser, jwtToken)

	if token.Email != testUser.Email {
		t.Error("Token not created properly")
	}
}
Example #3
0
// NewToken creates new instance of Token struct with
// given jwtToken, generated new value of "reference token"
func NewToken(user auth.User, jwtToken string) (Token, error) {
	token := Token{}
	jwtToken, err := jwt.GenerateJwtToken(user)
	if err != nil {
		return token, err
	}
	token.Id = bson.NewObjectId()
	token.JwtToken = jwtToken
	token.Email = user.Email
	token.RefToken = uuid.NewV4().String()
	token.Expiration = time.Now().Add(time.Hour * 72).Unix()
	return token, nil
}
Example #4
0
func TestMarshallUnmarshalBinary(t *testing.T) {

	jwtToken, err := jwt.GenerateJwtToken(testUser)
	if err != nil {
		t.Error(err)
	}
	token, _ := NewToken(testUser, jwtToken)

	serialized, tokenErr := token.MarshalBinary()
	if tokenErr != nil {
		t.Error(tokenErr)
	}

	deserializedToken := &Token{}
	deserializedToken.UnmarshalBinary(serialized)

	if token.JwtToken != deserializedToken.JwtToken {
		t.Error("Token do not serialize properly")
	}

}
Example #5
0
func loginHook(resp *natsproxy.Response) {

	userJSON := resp.GetHeader().Get(TokenHeader)
	user := auth.User{}
	json.Unmarshal([]byte(userJSON), &user)
	jwtToken, err := jwt.GenerateJwtToken(user)
	if err != nil {
		log.Error(err)
	}
	var token storage.Token
	if token, err = storage.NewToken(user, jwtToken); err != nil {
		log.Error(err)
		return
	}

	err = tokenStorage.InsertToken(token)
	if err != nil {
		log.Error(err)
		resp.StatusCode = http.StatusInternalServerError
		resp.GetHeader().Del(TokenHeader)
		return
	}
	resp.GetHeader().Set(TokenHeader, token.RefToken)
}