]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - api/routes.go
Mails.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / routes.go
CommitLineData
7a9e5112 1package api
2
3import (
4 "encoding/base64"
5
6 "github.com/gin-gonic/gin"
7)
8
9type Route struct {
10 Method string
11 Handlers []gin.HandlerFunc
12 Path string
13}
14
15type Group struct {
16 Root string
17 Middlewares []Middleware
18 Routes []Route
19}
20
21var Groups = []Group{
22 {
23 "",
24 nil,
25 []Route{
26 {"POST", []gin.HandlerFunc{Signup}, "/signup"},
27 {"POST", []gin.HandlerFunc{Signin}, "/signin"},
85545aba 28 {"POST", []gin.HandlerFunc{PasswordReset}, "/passwordreset"},
29 {"POST", []gin.HandlerFunc{ChangePassword}, "/changepassword"},
2da5b12c 30 {"POST", []gin.HandlerFunc{ConfirmEmail}, "/confirmemail"},
7a9e5112 31 },
32 },
33 {
34 "/otp",
35 []Middleware{JwtAuth, UserConfirmed},
36 []Route{
37 {"GET", []gin.HandlerFunc{OtpEnrollment}, "/enroll"},
38 {"POST", []gin.HandlerFunc{OtpValidate}, "/validate"},
39 },
40 },
41 {
42 "/market",
43 []Middleware{JwtAuth, UserConfirmed, OtpAuth},
44 []Route{
45 {"GET", []gin.HandlerFunc{GetMarketConfig}, "/:name"},
46 {"POST", []gin.HandlerFunc{UpdateMarketConfig}, "/:name/update"},
24e47979 47 {"GET", []gin.HandlerFunc{TestMarketCredentials}, "/:name/test-credentials"},
48 {"GET", []gin.HandlerFunc{GetPortfolio}, "/:name/portfolio"},
7a9e5112 49 },
50 },
51}
52
53func Signup(c *gin.Context) {
54 query := &SignupQuery{}
55
56 query.In.Email = c.PostForm("email")
57 query.In.Password = c.PostForm("password")
58
59 RunQuery(query, c)
60}
61
62func Signin(c *gin.Context) {
63 query := &SigninQuery{}
64
65 query.In.Email = c.PostForm("email")
66 query.In.Password = c.PostForm("password")
67
68 RunQuery(query, c)
69}
70
71func OtpValidate(c *gin.Context) {
72 query := &OtpValidateQuery{}
73
74 query.In.Pass = c.PostForm("pass")
75 query.In.User = GetUser(c)
76 query.In.Claims = GetClaims(c)
77
78 RunQuery(query, c)
79}
80
81func OtpEnrollment(c *gin.Context) {
82 query := &OtpEnrollmentQuery{}
83
84 query.In.User = GetUser(c)
85
86 qrPng, secret, err := query.Run()
87 if err != nil {
88 WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c)
89 c.Error(err)
90 return
91 }
92
93 if c.Query("format") == "png" {
94 c.Header("X-OTP-Secret", secret)
95 WriteBinary("image/png", qrPng.Bytes(), c)
96 } else {
97 response := struct {
98 Base64img string `json:"base64img"`
99 OtpSecret string `json:"secret"`
100 }{
101 base64.StdEncoding.EncodeToString(qrPng.Bytes()),
102 secret,
103 }
104
105 WriteJsonResponse(SuccessResponse(response), c)
106 }
107
108}
109
110func GetMarketConfig(c *gin.Context) {
111 query := &MarketConfigQuery{}
112
113 query.In.User = GetUser(c)
114 query.In.Market = c.Param("name")
115
116 RunQuery(query, c)
117}
118
24e47979 119func TestMarketCredentials(c *gin.Context) {
120 query := &TestMarketCredentialsQuery{}
121
122 query.In.User = GetUser(c)
123 query.In.Market = c.Param("name")
124
125 RunQuery(query, c)
126}
127
128func GetPortfolio(c *gin.Context) {
129 query := &GetPortfolioQuery{}
2f91f20a 130
131 query.In.User = GetUser(c)
132 query.In.Market = c.Param("name")
2f91f20a 133
134 RunQuery(query, c)
135}
136
7a9e5112 137func UpdateMarketConfig(c *gin.Context) {
138 query := &UpdateMarketConfigQuery{}
139
140 query.In.User = GetUser(c)
141 query.In.Market = c.Param("name")
142 query.In.Key = c.PostForm("key")
143 query.In.Secret = c.PostForm("secret")
144
145 RunQuery(query, c)
146}
85545aba 147
148func PasswordReset(c *gin.Context) {
149 query := &PasswordResetQuery{}
150
151 query.In.Email = c.PostForm("email")
152
153 RunQuery(query, c)
154}
155
156func ChangePassword(c *gin.Context) {
157 query := &ChangePasswordQuery{}
158
159 query.In.Token = c.PostForm("token")
160 query.In.Password = c.PostForm("password")
161
162 RunQuery(query, c)
163}
2da5b12c 164
165func ConfirmEmail(c *gin.Context) {
166 query := &ConfirmEmailQuery{}
167
168 query.In.Token = c.PostForm("token")
169
170 RunQuery(query, c)
171}