Exemplo n.º 1
0
func (suite *OauthTestSuite) TestNewIntrospectResponseFromRefreshToken() {
	refreshToken := &oauth.RefreshToken{
		Token:     "test_token_introspect_1",
		ExpiresAt: time.Now().UTC().Add(+10 * time.Second),
		ClientID:  util.PositiveIntOrNull(int64(suite.clients[0].ID)),
		UserID:    util.PositiveIntOrNull(int64(suite.users[0].ID)),
		Scope:     "read_write",
	}
	expected := &oauth.IntrospectResponse{
		Active:    true,
		Scope:     refreshToken.Scope,
		TokenType: tokentypes.Bearer,
		ExpiresAt: int(refreshToken.ExpiresAt.Unix()),
		ClientID:  suite.clients[0].Key,
		Username:  suite.users[0].Username,
	}

	actual, err := suite.service.NewIntrospectResponseFromRefreshToken(refreshToken)
	assert.NoError(suite.T(), err)
	assert.Equal(suite.T(), expected, actual)

	refreshToken.ClientID = util.PositiveIntOrNull(0)
	expected.ClientID = ""
	actual, err = suite.service.NewIntrospectResponseFromRefreshToken(refreshToken)
	assert.NoError(suite.T(), err)
	assert.Equal(suite.T(), expected, actual)

	refreshToken.UserID = util.PositiveIntOrNull(0)
	expected.Username = ""
	actual, err = suite.service.NewIntrospectResponseFromRefreshToken(refreshToken)
	assert.NoError(suite.T(), err)
	assert.Equal(suite.T(), expected, actual)
}
Exemplo n.º 2
0
func TestPositiveIntOrNull(t *testing.T) {
	var (
		nullInt sql.NullInt64
		value   driver.Value
		err     error
	)

	// When the number is negative
	nullInt = util.PositiveIntOrNull(-1)

	// nullInt.Valid should be false
	assert.False(t, nullInt.Valid)

	// nullInt.Value() should return nil
	value, err = nullInt.Value()
	assert.Nil(t, err)
	assert.Nil(t, value)

	// When the number is greater than zero
	nullInt = util.PositiveIntOrNull(1)

	// nullInt.Valid should be true
	assert.True(t, nullInt.Valid)

	// nullInt.Value() should return the integer
	value, err = nullInt.Value()
	assert.Nil(t, err)
	assert.Equal(t, int64(1), value)
}
Exemplo n.º 3
0
// NewAuthorizationCode creates new AuthorizationCode instance
func NewAuthorizationCode(client *Client, user *User, expiresIn int, redirectURI, scope string) *AuthorizationCode {
	return &AuthorizationCode{
		ClientID:    util.PositiveIntOrNull(int64(client.ID)),
		UserID:      util.PositiveIntOrNull(int64(user.ID)),
		Code:        uuid.New(),
		ExpiresAt:   time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
		RedirectURI: util.StringOrNull(redirectURI),
		Scope:       scope,
	}
}
Exemplo n.º 4
0
// NewAccessToken creates new AccessToken instance
func NewAccessToken(client *Client, user *User, expiresIn int, scope string) *AccessToken {
	accessToken := &AccessToken{
		ClientID:  util.PositiveIntOrNull(int64(client.ID)),
		Token:     uuid.New(),
		ExpiresAt: time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
		Scope:     scope,
	}
	if user != nil {
		accessToken.UserID = util.PositiveIntOrNull(int64(user.ID))
	}
	return accessToken
}
Exemplo n.º 5
0
// NewInvitation creates new Invitation instance
func NewInvitation(invitedUser, invitedByUser *User, expiresIn int) (*Invitation, error) {
	invitedUserID := util.PositiveIntOrNull(int64(invitedUser.ID))
	invitedByUserID := util.PositiveIntOrNull(int64(invitedByUser.ID))
	invitation := &Invitation{
		EmailTokenModel: EmailTokenModel{
			Reference:   uuid.New(),
			EmailSentAt: nil,
			ExpiresAt:   time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
		},
		InvitedUserID:   invitedUserID,
		InvitedByUserID: invitedByUserID,
	}
	return invitation, nil
}
Exemplo n.º 6
0
// NewUser creates new User instance
func NewUser(account *Account, oauthUser *oauth.User, facebookID string, confirmed bool, data *UserRequest) (*User, error) {
	accountID := util.PositiveIntOrNull(int64(account.ID))
	oauthUserID := util.PositiveIntOrNull(int64(oauthUser.ID))
	user := &User{
		AccountID:   accountID,
		OauthUserID: oauthUserID,
		FacebookID:  util.StringOrNull(facebookID),
		FirstName:   util.StringOrNull(data.FirstName),
		LastName:    util.StringOrNull(data.LastName),
		Picture:     util.StringOrNull(data.Picture),
		Confirmed:   confirmed,
	}
	return user, nil
}
Exemplo n.º 7
0
func TestIntOrNull(t *testing.T) {
	nullInt := util.PositiveIntOrNull(1)
	assert.True(t, nullInt.Valid)

	value, err := nullInt.Value()
	assert.Nil(t, err)
	assert.Equal(t, int64(1), value)
}
Exemplo n.º 8
0
// NewAccount creates new Account instance
func NewAccount(oauthClient *oauth.Client, name, description string) (*Account, error) {
	oauthClientID := util.PositiveIntOrNull(int64(oauthClient.ID))
	account := &Account{
		OauthClientID: oauthClientID,
		Name:          name,
		Description:   util.StringOrNull(description),
	}
	return account, nil
}
Exemplo n.º 9
0
// NewPasswordReset creates new PasswordReset instance
func NewPasswordReset(user *User, expiresIn int) (*PasswordReset, error) {
	userID := util.PositiveIntOrNull(int64(user.ID))
	passwordReset := &PasswordReset{
		EmailTokenModel: EmailTokenModel{
			Reference:   uuid.New(),
			EmailSentAt: nil,
			ExpiresAt:   time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
		},
		UserID: userID,
	}
	return passwordReset, nil
}
Exemplo n.º 10
0
// NewConfirmation creates new Confirmation instance
func NewConfirmation(user *User, expiresIn int) (*Confirmation, error) {
	userID := util.PositiveIntOrNull(int64(user.ID))
	confirmation := &Confirmation{
		EmailTokenModel: EmailTokenModel{
			Reference:   uuid.New(),
			EmailSentAt: nil,
			ExpiresAt:   time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
		},
		UserID: userID,
	}
	return confirmation, nil
}
Exemplo n.º 11
0
// FindAccountByOauthClientID looks up an account by oauth client ID and returns it
func (s *Service) FindAccountByOauthClientID(oauthClientID uint) (*Account, error) {
	// Fetch the client from the database
	account := new(Account)
	notFound := AccountPreload(s.db).Where(Account{
		OauthClientID: util.PositiveIntOrNull(int64(oauthClientID)),
	}).First(account).RecordNotFound()

	// Not found
	if notFound {
		return nil, ErrAccountNotFound
	}

	return account, nil
}
Exemplo n.º 12
0
// FindUserByOauthUserID looks up a user by oauth user ID and returns it
func (s *Service) FindUserByOauthUserID(oauthUserID uint) (*User, error) {
	// Fetch the user from the database
	user := new(User)
	notFound := UserPreload(s.db).Where(User{
		OauthUserID: util.PositiveIntOrNull(int64(oauthUserID)),
	}).First(user).RecordNotFound()

	// Not found
	if notFound {
		return nil, ErrUserNotFound
	}

	return user, nil
}