Skip to content
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
766 changes: 0 additions & 766 deletions api/user.go

This file was deleted.

88 changes: 88 additions & 0 deletions api/user/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package user

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// swagger:operation POST /api/v1/users users CreateUser
//
// Create a user for the configured backend
//
// ---
// produces:
// - application/json
// parameters:
// - in: body
// name: body
// description: Payload containing the user to create
// required: true
// schema:
// "$ref": "#/definitions/User"
// security:
// - ApiKeyAuth: []
// responses:
// '201':
// description: Successfully created the user
// schema:
// "$ref": "#/definitions/User"
// '400':
// description: Unable to create the user
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to create the user
// schema:
// "$ref": "#/definitions/Error"

// CreateUser represents the API handler to create
// a user in the configured backend.
func CreateUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)

// capture body from API request
input := new(library.User)

err := c.Bind(input)
if err != nil {
retErr := fmt.Errorf("unable to decode JSON for new user: %w", err)

util.HandleError(c, http.StatusBadRequest, retErr)

return
}

// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"user": u.GetName(),
}).Infof("creating new user %s", input.GetName())

// send API call to create the user
err = database.FromContext(c).CreateUser(input)
if err != nil {
retErr := fmt.Errorf("unable to create user: %w", err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

// send API call to capture the created user
user, _ := database.FromContext(c).GetUserForName(input.GetName())

c.JSON(http.StatusCreated, user)
}
78 changes: 78 additions & 0 deletions api/user/create_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

//nolint:dupl // ignore similar code with delete token
package user

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal/token"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// swagger:operation POST /api/v1/user/token users CreateToken
//
// Create a token for the current authenticated user
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully created a token for the current user
// schema:
// "$ref": "#/definitions/Token"
// '503':
// description: Unable to create a token for the current user
// schema:
// "$ref": "#/definitions/Error"

// CreateToken represents the API handler to create
// a user token in the configured backend.
func CreateToken(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)

// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"user": u.GetName(),
}).Infof("composing token for user %s", u.GetName())

tm := c.MustGet("token-manager").(*token.Manager)

// compose JWT token for user
rt, at, err := tm.Compose(c, u)
if err != nil {
retErr := fmt.Errorf("unable to compose token for user %s: %w", u.GetName(), err)

util.HandleError(c, http.StatusServiceUnavailable, retErr)

return
}

u.SetRefreshToken(rt)

// send API call to update the user
err = database.FromContext(c).UpdateUser(u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

util.HandleError(c, http.StatusServiceUnavailable, retErr)

return
}

c.JSON(http.StatusOK, library.Token{Token: &at})
}
82 changes: 82 additions & 0 deletions api/user/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package user

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
"github.com/sirupsen/logrus"
)

// swagger:operation DELETE /api/v1/users/{user} users DeleteUser
//
// Delete a user for the configured backend
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: user
// description: Name of the user
// required: true
// type: string
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully deleted of user
// schema:
// type: string
// '404':
// description: Unable to delete user
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to delete user
// schema:
// "$ref": "#/definitions/Error"

// DeleteUser represents the API handler to remove
// a user from the configured backend.
func DeleteUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
user := util.PathParameter(c, "user")

// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"user": u.GetName(),
}).Infof("deleting user %s", user)

// send API call to capture the user
u, err := database.FromContext(c).GetUserForName(user)
if err != nil {
retErr := fmt.Errorf("unable to get user %s: %w", user, err)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

// send API call to remove the user
err = database.FromContext(c).DeleteUser(u)
if err != nil {
retErr := fmt.Errorf("unable to delete user %s: %w", u.GetName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

c.JSON(http.StatusOK, fmt.Sprintf("user %s deleted", u.GetName()))
}
78 changes: 78 additions & 0 deletions api/user/delete_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

//nolint:dupl // ignore similar code with create token
package user

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal/token"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// swagger:operation DELETE /api/v1/user/token users DeleteToken
//
// Delete a token for the current authenticated user
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully delete a token for the current user
// schema:
// type: string
// '500':
// description: Unable to delete a token for the current user
// schema:
// "$ref": "#/definitions/Error"

// DeleteToken represents the API handler to revoke
// and recreate a user token in the configured backend.
func DeleteToken(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)

// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"user": u.GetName(),
}).Infof("revoking token for user %s", u.GetName())

tm := c.MustGet("token-manager").(*token.Manager)

// compose JWT token for user
rt, at, err := tm.Compose(c, u)
if err != nil {
retErr := fmt.Errorf("unable to compose token for user %s: %w", u.GetName(), err)

util.HandleError(c, http.StatusServiceUnavailable, retErr)

return
}

u.SetRefreshToken(rt)

// send API call to update the user
err = database.FromContext(c).UpdateUser(u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

util.HandleError(c, http.StatusServiceUnavailable, retErr)

return
}

c.JSON(http.StatusOK, library.Token{Token: &at})
}
68 changes: 68 additions & 0 deletions api/user/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package user

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
"github.com/sirupsen/logrus"
)

// swagger:operation GET /api/v1/users/{user} users GetUser
//
// Retrieve a user for the configured backend
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: user
// description: Name of the user
// required: true
// type: string
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved the user
// schema:
// "$ref": "#/definitions/User"
// '404':
// description: Unable to retrieve the user
// schema:
// "$ref": "#/definitions/Error"

// GetUser represents the API handler to capture a
// user from the configured backend.
func GetUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
user := util.PathParameter(c, "user")

// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"user": u.GetName(),
}).Infof("reading user %s", user)

// send API call to capture the user
u, err := database.FromContext(c).GetUserForName(user)
if err != nil {
retErr := fmt.Errorf("unable to get user %s: %w", user, err)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

c.JSON(http.StatusOK, u)
}
Loading