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(user_profile): Add endpoint to fetch user profile #97

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,47 @@ const docTemplate = `{
}
}
},
"/users/profile": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Get user's own profile",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Users"
],
"summary": "Get user's own profile",
"operationId": "GetUserProfile",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.UserResponse"
}
},
"400": {
"description": "Invalid user",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
},
"404": {
"description": "User not found",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
},
"/users/{username}": {
"get": {
"security": [
Expand Down
41 changes: 41 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,47 @@
}
}
},
"/users/profile": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Get user's own profile",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Users"
],
"summary": "Get user's own profile",
"operationId": "GetUserProfile",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.UserResponse"
}
},
"400": {
"description": "Invalid user",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
},
"404": {
"description": "User not found",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
},
"/users/{username}": {
"get": {
"security": [
Expand Down
26 changes: 26 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,32 @@ paths:
summary: Create new user via oidc id token
tags:
- Users
/users/profile:
get:
consumes:
- application/json
description: Get user's own profile
operationId: GetUserProfile
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.UserResponse'
"400":
description: Invalid user
schema:
$ref: '#/definitions/models.LicenseError'
"404":
description: User not found
schema:
$ref: '#/definitions/models.LicenseError'
security:
- ApiKeyAuth: []
summary: Get user's own profile
tags:
- Users
securityDefinitions:
ApiKeyAuth:
description: Token from /login endpoint. Enter the token with the `Bearer ` prefix,
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func Router() *gin.Engine {
users := authorizedv1.Group("/users")
{
users.GET("", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.GetAllUser)
users.GET("/profile", auth.GetUserProfile)
users.GET(":username", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.GetUser)
users.POST("", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.CreateUser)
users.PATCH("", auth.UpdateProfile)
Expand Down Expand Up @@ -226,6 +227,7 @@ func Router() *gin.Engine {
users := authorizedv1.Group("/users")
{
users.GET("", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.GetAllUser)
users.GET("/profile", auth.GetUserProfile)
users.GET(":username", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.GetUser)
users.POST("", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.CreateUser)
users.PATCH(":username", middleware.RoleBasedAccessMiddleware([]string{"ADMIN"}), auth.UpdateUser)
Expand Down
41 changes: 41 additions & 0 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,47 @@ func Login(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"token": token})
}

// GetUserProfile retrieves the user's own profile.
//
// @Summary Get user's own profile
// @Description Get user's own profile
// @Id GetUserProfile
// @Tags Users
// @Accept json
// @Produce json
// @Success 200 {object} models.UserResponse
// @Failure 400 {object} models.LicenseError "Invalid user"
// @Failure 404 {object} models.LicenseError "User not found"
// @Security ApiKeyAuth
// @Router /users/profile [get]
func GetUserProfile(c *gin.Context) {
var user models.User
username := c.GetString("username")

active := true
if err := db.DB.Where(models.User{Username: &username, Active: &active}).First(&user).Error; err != nil {
er := models.LicenseError{
Status: http.StatusNotFound,
Message: "no user with such username exists",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusNotFound, er)
return
}

res := models.UserResponse{
Data: []models.User{user},
Status: http.StatusOK,
Meta: &models.PaginationMeta{
ResourceCount: 1,
},
}

c.JSON(http.StatusOK, res)
}

// encryptUserPassword checks if the password is already encrypted or not. If
// not, it encrypts the password.
func encryptUserPassword(user *models.User) error {
Expand Down
Loading