Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ID method to auth #616

Merged
merged 14 commits into from
Sep 4, 2024
12 changes: 12 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ func (a *Auth) User(user any) error {
return nil
}

func (a *Auth) Id() (string, error) {
auth, ok := a.ctx.Value(ctxKey).(Guards)
if !ok || auth[a.guard] == nil {
return "", ErrorParseTokenFirst
}
if auth[a.guard].Token == "" {
return "", ErrorTokenExpired
}

return auth[a.guard].Claims.Key, nil
}

func (a *Auth) Parse(token string) (*contractsauth.Payload, error) {
token = strings.ReplaceAll(token, "Bearer ", "")
if a.cache == nil {
Expand Down
64 changes: 64 additions & 0 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,70 @@ func (s *AuthTestSuite) TestUser_NoParse() {
s.mockConfig.AssertExpectations(s.T())
}

func (s *AuthTestSuite) TestID_NoParse() {
// Attempt to get the ID without parsing the token first
id, _ := s.auth.Id()
s.Empty(id)
}

func (s *AuthTestSuite) TestID_Success() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

// Log in to get a token
token, err := s.auth.LoginUsingID(1)
s.Nil(err)

s.mockCache.On("GetBool", "jwt:disabled:"+token, false).Return(false).Once()

// Parse the token
payload, err := s.auth.Parse(token)
s.Nil(err)
s.NotNil(payload)

// Now, call the ID method and expect it to return the correct ID
id, _ := s.auth.Id()
s.Equal("1", id)
}

func (s *AuthTestSuite) TestID_TokenExpired() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

// Log in to get a token
token, err := s.auth.LoginUsingID(1)
s.Nil(err)

// Set the token as expired
carbon.SetTestNow(carbon.Now().AddMinutes(3))

s.mockCache.On("GetBool", "jwt:disabled:"+token, false).Return(false).Once()

// Parse the token
_, err = s.auth.Parse(token)
s.ErrorIs(err, ErrorTokenExpired)

// Now, call the ID method and expect it to return an empty value
id, _ := s.auth.Id()
s.Empty(id)

carbon.UnsetTestNow()
}

func (s *AuthTestSuite) TestID_TokenInvalid() {
// Simulate an invalid token scenario
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Once()

token := "invalidToken"
s.mockCache.On("GetBool", "jwt:disabled:"+token, false).Return(false).Once()

_, err := s.auth.Parse(token)
s.ErrorIs(err, ErrorInvalidToken)

id, _ := s.auth.Id()
s.Empty(id)
}

func (s *AuthTestSuite) TestUser_DBError() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()
Expand Down
2 changes: 2 additions & 0 deletions contracts/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Auth interface {
Parse(token string) (*Payload, error)
// User returns the current authenticated user.
User(user any) error
// Id returns the current user id.
Id() (string, error)
// Login logs a user into the application.
Login(user any) (token string, err error)
// LoginUsingID logs the given user ID into the application.
Expand Down
Loading