aboutsummaryrefslogblamecommitdiff
path: root/api/routes.go
blob: d0e8cec151dbd550e2c406f06a7498747c9d1207 (plain) (tree)


























                                                                       

                                                                                       
                                                                                   















                                                                                         

                                                                                                     





































































                                                                              










                                              


                                         



                          









                                              
















                                                  







                                            
package api

import (
	"encoding/base64"

	"github.com/gin-gonic/gin"
)

type Route struct {
	Method   string
	Handlers []gin.HandlerFunc
	Path     string
}

type Group struct {
	Root        string
	Middlewares []Middleware
	Routes      []Route
}

var Groups = []Group{
	{
		"",
		nil,
		[]Route{
			{"POST", []gin.HandlerFunc{Signup}, "/signup"},
			{"POST", []gin.HandlerFunc{Signin}, "/signin"},
			{"POST", []gin.HandlerFunc{PasswordReset}, "/passwordreset"},
			{"POST", []gin.HandlerFunc{ChangePassword}, "/changepassword"},
			{"POST", []gin.HandlerFunc{ConfirmEmail}, "/confirmemail"},
		},
	},
	{
		"/otp",
		[]Middleware{JwtAuth, UserConfirmed},
		[]Route{
			{"GET", []gin.HandlerFunc{OtpEnrollment}, "/enroll"},
			{"POST", []gin.HandlerFunc{OtpValidate}, "/validate"},
		},
	},
	{
		"/market",
		[]Middleware{JwtAuth, UserConfirmed, OtpAuth},
		[]Route{
			{"GET", []gin.HandlerFunc{GetMarketConfig}, "/:name"},
			{"POST", []gin.HandlerFunc{UpdateMarketConfig}, "/:name/update"},
			{"GET", []gin.HandlerFunc{TestMarketCredentials}, "/:name/test-credentials"},
			{"GET", []gin.HandlerFunc{GetPortfolio}, "/:name/portfolio"},
		},
	},
}

func Signup(c *gin.Context) {
	query := &SignupQuery{}

	query.In.Email = c.PostForm("email")
	query.In.Password = c.PostForm("password")

	RunQuery(query, c)
}

func Signin(c *gin.Context) {
	query := &SigninQuery{}

	query.In.Email = c.PostForm("email")
	query.In.Password = c.PostForm("password")

	RunQuery(query, c)
}

func OtpValidate(c *gin.Context) {
	query := &OtpValidateQuery{}

	query.In.Pass = c.PostForm("pass")
	query.In.User = GetUser(c)
	query.In.Claims = GetClaims(c)

	RunQuery(query, c)
}

func OtpEnrollment(c *gin.Context) {
	query := &OtpEnrollmentQuery{}

	query.In.User = GetUser(c)

	qrPng, secret, err := query.Run()
	if err != nil {
		WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c)
		c.Error(err)
		return
	}

	if c.Query("format") == "png" {
		c.Header("X-OTP-Secret", secret)
		WriteBinary("image/png", qrPng.Bytes(), c)
	} else {
		response := struct {
			Base64img string `json:"base64img"`
			OtpSecret string `json:"secret"`
		}{
			base64.StdEncoding.EncodeToString(qrPng.Bytes()),
			secret,
		}

		WriteJsonResponse(SuccessResponse(response), c)
	}

}

func GetMarketConfig(c *gin.Context) {
	query := &MarketConfigQuery{}

	query.In.User = GetUser(c)
	query.In.Market = c.Param("name")

	RunQuery(query, c)
}

func TestMarketCredentials(c *gin.Context) {
	query := &TestMarketCredentialsQuery{}

	query.In.User = GetUser(c)
	query.In.Market = c.Param("name")

	RunQuery(query, c)
}

func GetPortfolio(c *gin.Context) {
	query := &GetPortfolioQuery{}

	query.In.User = GetUser(c)
	query.In.Market = c.Param("name")

	RunQuery(query, c)
}

func UpdateMarketConfig(c *gin.Context) {
	query := &UpdateMarketConfigQuery{}

	query.In.User = GetUser(c)
	query.In.Market = c.Param("name")
	query.In.Key = c.PostForm("key")
	query.In.Secret = c.PostForm("secret")

	RunQuery(query, c)
}

func PasswordReset(c *gin.Context) {
	query := &PasswordResetQuery{}

	query.In.Email = c.PostForm("email")

	RunQuery(query, c)
}

func ChangePassword(c *gin.Context) {
	query := &ChangePasswordQuery{}

	query.In.Token = c.PostForm("token")
	query.In.Password = c.PostForm("password")

	RunQuery(query, c)
}

func ConfirmEmail(c *gin.Context) {
	query := &ConfirmEmailQuery{}

	query.In.Token = c.PostForm("token")

	RunQuery(query, c)
}