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
18 changes: 18 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@
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].Claims == nil {
return "", ErrorParseTokenFirst

Check warning on line 85 in auth/auth.go

View check run for this annotation

Codecov / codecov/patch

auth/auth.go#L85

Added line #L85 was not covered by tests
}
if auth[a.guard].Claims.Key == "" {
return "", ErrorInvalidKey

Check warning on line 88 in auth/auth.go

View check run for this annotation

Codecov / codecov/patch

auth/auth.go#L88

Added line #L88 was not covered by tests
}
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
Loading