aboutsummaryrefslogtreecommitdiff
path: root/api/routes.go
blob: d7e712c7c5f0495717ef850e88695a401781037c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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"},
		},
	},
	{
		"/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"},
		},
	},
}

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