-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: complete support for this service (#37)
* fix: added missing env setup in dockerfile related to JWT secret * feat: authorize controller operation * feat: authorization prefix * feat: update password * feat: challenge endpoint * docs: fixed documentation of the specification * feat: user uuid by username * docs: added documentation for JWT secret flag * chore(release): 0.0.22
- Loading branch information
Showing
25 changed files
with
600 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package controller | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/hawks-atlanta/authentication-go/models" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func (c *Controller) Authorize(jwtS string) (user models.User, err error) { | ||
tok, err := jwt.Parse(jwtS, c.JWT.KeyFunc) | ||
if err != nil { | ||
err = ErrUnauthorized | ||
return user, err | ||
} | ||
var check models.User | ||
err = check.FromClaims(tok.Claims.(jwt.MapClaims)) | ||
if err != nil { | ||
err = ErrUnauthorized | ||
return user, err | ||
} | ||
err = c.DB. | ||
Where("uuid = ?", check.UUID). | ||
First(&user). | ||
Error | ||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { | ||
err = ErrUnauthorized | ||
} | ||
return user, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package controller | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hawks-atlanta/authentication-go/database" | ||
"github.com/hawks-atlanta/authentication-go/internal/utils/jwt" | ||
"github.com/hawks-atlanta/authentication-go/internal/utils/random" | ||
"github.com/hawks-atlanta/authentication-go/models" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func TestController_Authorize(t *testing.T) { | ||
t.Run("Succeed", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
|
||
c, err := New(WithDB(db), WithSecret(random.Bytes(16))) | ||
assertions.Nil(err) | ||
|
||
u := models.RandomUser() | ||
err = c.Register(u) | ||
assertions.Nil(err) | ||
|
||
token := c.JWT.New(u.Claims()) | ||
|
||
user, err := c.Authorize(token) | ||
assertions.Nil(err) | ||
|
||
assertions.Equal(u.UUID, user.UUID) | ||
assertions.Equal(u.Username, user.Username) | ||
})) | ||
t.Run("Invalid Token", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
|
||
c, err := New(WithDB(db), WithSecret(random.Bytes(16))) | ||
assertions.Nil(err) | ||
|
||
_, err = c.Authorize("INVALID") | ||
assertions.NotNil(err) | ||
})) | ||
t.Run("Invalid SECRET", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
|
||
c, err := New(WithDB(db), WithSecret(random.Bytes(16))) | ||
assertions.Nil(err) | ||
|
||
_, err = c.Authorize(jwt.New(random.Bytes(16)).New(models.RandomUser().Claims())) | ||
assertions.NotNil(err) | ||
})) | ||
t.Run("Invalid User", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
|
||
c, err := New(WithDB(db), WithSecret(random.Bytes(16))) | ||
assertions.Nil(err) | ||
|
||
_, err = c.Authorize(c.JWT.New(models.RandomUser().Claims())) | ||
assertions.NotNil(err) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package controller | ||
|
||
import "github.com/hawks-atlanta/authentication-go/models" | ||
|
||
type UserRequest struct { | ||
Username string `json:"username"` | ||
} | ||
|
||
func (c *Controller) UserByUsername(req *UserRequest) (user models.User, err error) { | ||
err = c.DB. | ||
Where("username = ?", req.Username). | ||
First(&user). | ||
Error | ||
return user, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package controller | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hawks-atlanta/authentication-go/database" | ||
"github.com/hawks-atlanta/authentication-go/internal/utils/random" | ||
"github.com/hawks-atlanta/authentication-go/models" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func TestUserByUsername(t *testing.T) { | ||
t.Run("Succeed", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
|
||
c, err := New(WithDB(db), WithSecret(random.String(16))) | ||
assertions.Nil(err) | ||
|
||
u := models.RandomUser() | ||
err = c.Register(u) | ||
assertions.Nil(err) | ||
|
||
req := UserRequest{ | ||
Username: *u.Username, | ||
} | ||
ou, err := c.UserByUsername(&req) | ||
|
||
assertions.Equal(u.UUID, ou.UUID) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/hawks-atlanta/authentication-go/models" | ||
) | ||
|
||
type UpdatePasswordRequest struct { | ||
OldPassword string `json:"oldPassword"` | ||
NewPassword string `json:"newPassword"` | ||
} | ||
|
||
func (c *Controller) UpdatePassword(session *models.User, req *UpdatePasswordRequest) (err error) { | ||
if !session.Authenticate(req.OldPassword) { | ||
return ErrUnauthorized | ||
} | ||
update := models.User{ | ||
Model: session.Model, | ||
Password: req.NewPassword, | ||
} | ||
err = c.DB. | ||
Where("uuid = ? AND password_hash = ?", session.UUID, session.PasswordHash). | ||
Updates(&update). | ||
Error | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package controller | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hawks-atlanta/authentication-go/database" | ||
"github.com/hawks-atlanta/authentication-go/internal/utils/random" | ||
"github.com/hawks-atlanta/authentication-go/models" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func TestController_UpdatePassword(t *testing.T) { | ||
t.Run("Succeed", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
|
||
c, err := New(WithDB(db), WithSecret(random.Bytes(16))) | ||
assertions.Nil(err) | ||
|
||
u := models.RandomUser() | ||
err = c.Register(u) | ||
assertions.Nil(err) | ||
|
||
req := UpdatePasswordRequest{ | ||
OldPassword: u.Password, | ||
NewPassword: random.String(16), | ||
} | ||
err = c.UpdatePassword(u, &req) | ||
assertions.Nil(err) | ||
|
||
creds := models.User{ | ||
Username: u.Username, | ||
Password: req.NewPassword, | ||
} | ||
_, err = c.Login(&creds) | ||
assertions.Nil(err) | ||
})) | ||
t.Run("Invalid Password", database.Test(func(t *testing.T, db *gorm.DB) { | ||
assertions := assert.New(t) | ||
|
||
c, err := New(WithDB(db), WithSecret(random.Bytes(16))) | ||
assertions.Nil(err) | ||
|
||
u := models.RandomUser() | ||
err = c.Register(u) | ||
assertions.Nil(err) | ||
|
||
req := UpdatePasswordRequest{ | ||
OldPassword: random.String(16), | ||
NewPassword: random.String(16), | ||
} | ||
err = c.UpdatePassword(u, &req) | ||
assertions.NotNil(err) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
"git-semver-tags": "^4.1.1", | ||
"standard-version": "^9.5.0" | ||
}, | ||
"version": "0.0.21" | ||
"version": "0.0.22" | ||
} |
Oops, something went wrong.