98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
// Copyright 2024 perp (supernets)
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package account
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.supernets.org/perp/gopay/internal/context"
|
|
"git.supernets.org/perp/gopay/internal/jwt"
|
|
v1 "git.supernets.org/perp/gopay/internal/models/v1"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// @summary Account registration
|
|
// @description Register an account
|
|
// @tags account
|
|
// @accept json
|
|
// @produce json
|
|
// @param register body v1.Register true "alice" "supersecretpassword"
|
|
// @success 200 {object} models.Token
|
|
// @failure 400 {object} models.Error "MissingBody | UsernameTaken"
|
|
// @failure 403 {object} models.Error "RegistrationDisabled"
|
|
// @failure 500 {object} models.Error "InternalServerError"
|
|
// @router /v1/account/register [post]
|
|
func Register(ctx *context.Context) {
|
|
// Check if registration is disabled
|
|
if ctx.Config.Auth.Disabled {
|
|
ctx.JSON(403, ctx.Error("RegistrationDisabled"))
|
|
return
|
|
}
|
|
|
|
// Store body
|
|
var body *v1.Register
|
|
|
|
// Bind JSON
|
|
err := ctx.BindJSON(&body)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
ctx.JSON(400, ctx.Error("MissingBody"))
|
|
return
|
|
}
|
|
|
|
// Select account by username
|
|
account, err := ctx.Db.Account.SelectByUsername(body.Username)
|
|
if err != nil {
|
|
ctx.JSON(500, ctx.Error("InternalServerError"))
|
|
return
|
|
}
|
|
|
|
// Account exists
|
|
if account.Username != "" {
|
|
ctx.JSON(400, ctx.Error("UsernameTaken"))
|
|
return
|
|
}
|
|
|
|
// Hash password
|
|
password, err := bcrypt.GenerateFromPassword([]byte(body.Password), ctx.Config.Auth.Cost)
|
|
if err != nil {
|
|
ctx.JSON(500, ctx.Error("InternalServerError"))
|
|
return
|
|
}
|
|
|
|
// Insert account
|
|
err = ctx.Db.Account.Insert(body.Username, string(password))
|
|
if err != nil {
|
|
ctx.JSON(500, ctx.Error("InternalServerError"))
|
|
return
|
|
}
|
|
|
|
// Select account by username
|
|
account, err = ctx.Db.Account.SelectByUsername(body.Username)
|
|
if err != nil {
|
|
ctx.JSON(500, ctx.Error("InternalServerError"))
|
|
return
|
|
}
|
|
|
|
// Generate token
|
|
token, err := jwt.Encode(account.ID)
|
|
if err != nil {
|
|
ctx.JSON(500, ctx.Error("InternalServerError"))
|
|
return
|
|
}
|
|
|
|
ctx.JSON(200, ctx.Token(token))
|
|
}
|