feat: initial commit

This commit is contained in:
2023-11-15 20:38:25 +01:00
commit e199fe3d26
67 changed files with 4152 additions and 0 deletions

View 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)
}

View File

@ -0,0 +1,8 @@
package api
import "gitlab.com/wpetit/goweb/api"
var (
ErrCodeAlreadyExist api.ErrorCode = "already-exist"
ErrNotModified api.ErrorCode = "not-modified"
)

View 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)
}

View 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)
}

View 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)
}

View 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)
}

View 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
}

View 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)
}