feat: initial commit
This commit is contained in:
52
internal/route/api/create_user.go
Normal file
52
internal/route/api/create_user.go
Normal file
@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/hydra-webauthn/internal/storage"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
)
|
||||
|
||||
type CreateUserRequest struct {
|
||||
Username string `json:"username" validate:"required"`
|
||||
Attributes map[string]any `json:"attributes"`
|
||||
}
|
||||
|
||||
type CreateUserResponse struct {
|
||||
User *storage.User `json:"user"`
|
||||
}
|
||||
|
||||
func CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
createUserReq := &CreateUserRequest{}
|
||||
if ok := api.Bind(w, r, createUserReq); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
ctn := container.Must(ctx)
|
||||
storageSrv := storage.Must(ctn)
|
||||
|
||||
user, err := storageSrv.User().CreateUser(ctx, createUserReq.Username, createUserReq.Attributes)
|
||||
if err != nil {
|
||||
if errors.Is(err, storage.ErrAlreadyExist) {
|
||||
logger.Warn(ctx, "user already exists", logger.E(errors.WithStack(err)), logger.F("username", createUserReq.Username))
|
||||
api.ErrorResponse(w, http.StatusConflict, ErrCodeAlreadyExist, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
logger.Error(ctx, "could not create user", logger.E(errors.WithStack(err)))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
response := CreateUserResponse{
|
||||
User: user,
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusCreated, response)
|
||||
}
|
8
internal/route/api/error.go
Normal file
8
internal/route/api/error.go
Normal file
@ -0,0 +1,8 @@
|
||||
package api
|
||||
|
||||
import "gitlab.com/wpetit/goweb/api"
|
||||
|
||||
var (
|
||||
ErrCodeAlreadyExist api.ErrorCode = "already-exist"
|
||||
ErrNotModified api.ErrorCode = "not-modified"
|
||||
)
|
45
internal/route/api/generate_registration_link.go
Normal file
45
internal/route/api/generate_registration_link.go
Normal file
@ -0,0 +1,45 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/hydra-webauthn/internal/storage"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
)
|
||||
|
||||
type GenerateRegistrationLinkResponse struct {
|
||||
RegistrationLink *storage.RegistrationLink `json:"registrationLink"`
|
||||
}
|
||||
|
||||
func GenerateRegistrationLink(w http.ResponseWriter, r *http.Request) {
|
||||
userID := chi.URLParam(r, "userID")
|
||||
|
||||
ctx := r.Context()
|
||||
ctn := container.Must(ctx)
|
||||
storageSrv := storage.Must(ctn)
|
||||
|
||||
registrationLink, err := storageSrv.User().GenerateRegistrationLink(ctx, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
logger.Warn(ctx, "user not found", logger.E(errors.WithStack(err)), logger.F("userID", userID))
|
||||
api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
logger.Error(ctx, "could not generate registration link", logger.E(errors.WithStack(err)))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
response := GenerateRegistrationLinkResponse{
|
||||
RegistrationLink: registrationLink,
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusCreated, response)
|
||||
}
|
45
internal/route/api/get_registration_link.go
Normal file
45
internal/route/api/get_registration_link.go
Normal file
@ -0,0 +1,45 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/hydra-webauthn/internal/storage"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
)
|
||||
|
||||
type GetRegistrationLinkResponse struct {
|
||||
RegistrationLink *storage.RegistrationLink `json:"registrationLink"`
|
||||
}
|
||||
|
||||
func GetRegistrationLink(w http.ResponseWriter, r *http.Request) {
|
||||
userID := chi.URLParam(r, "userID")
|
||||
|
||||
ctx := r.Context()
|
||||
ctn := container.Must(ctx)
|
||||
storageSrv := storage.Must(ctn)
|
||||
|
||||
registrationLink, err := storageSrv.User().GetRegistrationLink(ctx, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
logger.Warn(ctx, "registration link or associated user not found", logger.E(errors.WithStack(err)), logger.F("userID", userID))
|
||||
api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
logger.Error(ctx, "could not get registration link", logger.E(errors.WithStack(err)))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
response := GetRegistrationLinkResponse{
|
||||
RegistrationLink: registrationLink,
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, response)
|
||||
}
|
45
internal/route/api/get_user.go
Normal file
45
internal/route/api/get_user.go
Normal file
@ -0,0 +1,45 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/hydra-webauthn/internal/storage"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
)
|
||||
|
||||
type GetUserResponse struct {
|
||||
User *storage.User `json:"user"`
|
||||
}
|
||||
|
||||
func GetUser(w http.ResponseWriter, r *http.Request) {
|
||||
userID := chi.URLParam(r, "userID")
|
||||
|
||||
ctx := r.Context()
|
||||
ctn := container.Must(ctx)
|
||||
storageSrv := storage.Must(ctn)
|
||||
|
||||
user, err := storageSrv.User().FindUserByID(ctx, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
logger.Warn(ctx, "could not find user", logger.E(errors.WithStack(err)), logger.F("userID", userID))
|
||||
api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
logger.Error(ctx, "could not get user", logger.E(errors.WithStack(err)))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
response := GetUserResponse{
|
||||
User: user,
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, response)
|
||||
}
|
42
internal/route/api/list_users.go
Normal file
42
internal/route/api/list_users.go
Normal file
@ -0,0 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/hydra-webauthn/internal/storage"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
)
|
||||
|
||||
type ListUsersResponse struct {
|
||||
Users []storage.UserHeader `json:"users"`
|
||||
}
|
||||
|
||||
func ListUsers(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
ctn := container.Must(ctx)
|
||||
storageSrv := storage.Must(ctn)
|
||||
|
||||
users, err := storageSrv.User().ListUsers(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
logger.Warn(ctx, "could not list users", logger.E(errors.WithStack(err)))
|
||||
api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
logger.Error(ctx, "could not get user", logger.E(errors.WithStack(err)))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
response := ListUsersResponse{
|
||||
Users: users,
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, response)
|
||||
}
|
33
internal/route/api/mount.go
Normal file
33
internal/route/api/mount.go
Normal file
@ -0,0 +1,33 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"forge.cadoles.com/wpetit/hydra-webauthn/internal/config"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
)
|
||||
|
||||
func Mount(r *chi.Mux, config *config.Config) error {
|
||||
basicAuth := middleware.BasicAuth("Hydra WebAuthn", toCredentials(config.API.Authentication.Accounts))
|
||||
|
||||
r.Route("/api/v1", func(r chi.Router) {
|
||||
r.Use(basicAuth)
|
||||
r.Post("/users", CreateUser)
|
||||
r.Get("/users/{userID}", GetUser)
|
||||
r.Get("/users", ListUsers)
|
||||
r.Post("/users/{userID}/registration-link", GenerateRegistrationLink)
|
||||
r.Get("/users/{userID}/registration-link", GetRegistrationLink)
|
||||
r.Put("/users/{userID}", UpdateUser)
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func toCredentials(accounts []config.AccountConfig) map[string]string {
|
||||
credentials := make(map[string]string, len(accounts))
|
||||
|
||||
for _, acc := range accounts {
|
||||
credentials[acc.Username] = acc.Password
|
||||
}
|
||||
|
||||
return credentials
|
||||
}
|
85
internal/route/api/update_user.go
Normal file
85
internal/route/api/update_user.go
Normal file
@ -0,0 +1,85 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/hydra-webauthn/internal/storage"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/api"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
)
|
||||
|
||||
type UpdateUserRequest struct {
|
||||
Username *string `json:"username"`
|
||||
Attributes *map[string]any `json:"attributes"`
|
||||
}
|
||||
|
||||
type UpdateUserResponse struct {
|
||||
User *storage.User `json:"user"`
|
||||
}
|
||||
|
||||
func UpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
userID := chi.URLParam(r, "userID")
|
||||
|
||||
updateUserReq := &UpdateUserRequest{}
|
||||
if ok := api.Bind(w, r, updateUserReq); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
ctn := container.Must(ctx)
|
||||
storageSrv := storage.Must(ctn)
|
||||
|
||||
var (
|
||||
user *storage.User
|
||||
err error
|
||||
)
|
||||
|
||||
if updateUserReq.Username != nil {
|
||||
user, err = storageSrv.User().UpdateUserUsername(ctx, userID, *updateUserReq.Username)
|
||||
if err != nil {
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
logger.Warn(ctx, "could not find user", logger.E(errors.WithStack(err)), logger.F("userID", userID))
|
||||
api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
logger.Error(ctx, "could not update username", logger.E(errors.WithStack(err)))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if updateUserReq.Attributes != nil {
|
||||
user, err = storageSrv.User().UpdateUserAttributes(ctx, userID, *updateUserReq.Attributes)
|
||||
if err != nil {
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
logger.Warn(ctx, "could not find user", logger.E(errors.WithStack(err)), logger.F("userID", userID))
|
||||
api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
logger.Error(ctx, "could not update attributes", logger.E(errors.WithStack(err)), logger.F("userID", userID))
|
||||
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if user == nil {
|
||||
api.ErrorResponse(w, http.StatusBadRequest, ErrNotModified, nil)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
response := UpdateUserResponse{
|
||||
User: user,
|
||||
}
|
||||
|
||||
api.DataResponse(w, http.StatusOK, response)
|
||||
}
|
Reference in New Issue
Block a user