]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - api/routes.go
Account information panel.
[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 },
d1c0ccfc 51 {
52 "/user",
53 []Middleware{JwtAuth, UserConfirmed, OtpAuth},
54 []Route{
55 {"GET", []gin.HandlerFunc{UserAccount}, "/account"},
56 },
57 },
7a9e5112 58}
59
60func Signup(c *gin.Context) {
61 query := &SignupQuery{}
62
63 query.In.Email = c.PostForm("email")
64 query.In.Password = c.PostForm("password")
65
66 RunQuery(query, c)
67}
68
69func Signin(c *gin.Context) {
70 query := &SigninQuery{}
71
72 query.In.Email = c.PostForm("email")
73 query.In.Password = c.PostForm("password")
74
75 RunQuery(query, c)
76}
77
78func OtpValidate(c *gin.Context) {
79 query := &OtpValidateQuery{}
80
81 query.In.Pass = c.PostForm("pass")
82 query.In.User = GetUser(c)
83 query.In.Claims = GetClaims(c)
84
85 RunQuery(query, c)
86}
87
88func OtpEnrollment(c *gin.Context) {
89 query := &OtpEnrollmentQuery{}
90
91 query.In.User = GetUser(c)
92
93 qrPng, secret, err := query.Run()
94 if err != nil {
95 WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c)
96 c.Error(err)
97 return
98 }
99
100 if c.Query("format") == "png" {
101 c.Header("X-OTP-Secret", secret)
102 WriteBinary("image/png", qrPng.Bytes(), c)
103 } else {
104 response := struct {
105 Base64img string `json:"base64img"`
106 OtpSecret string `json:"secret"`
107 }{
108 base64.StdEncoding.EncodeToString(qrPng.Bytes()),
109 secret,
110 }
111
112 WriteJsonResponse(SuccessResponse(response), c)
113 }
114
115}
116
117func GetMarketConfig(c *gin.Context) {
118 query := &MarketConfigQuery{}
119
120 query.In.User = GetUser(c)
121 query.In.Market = c.Param("name")
122
123 RunQuery(query, c)
124}
125
24e47979 126func TestMarketCredentials(c *gin.Context) {
127 query := &TestMarketCredentialsQuery{}
128
129 query.In.User = GetUser(c)
130 query.In.Market = c.Param("name")
131
132 RunQuery(query, c)
133}
134
135func GetPortfolio(c *gin.Context) {
136 query := &GetPortfolioQuery{}
2f91f20a 137
138 query.In.User = GetUser(c)
139 query.In.Market = c.Param("name")
2f91f20a 140
141 RunQuery(query, c)
142}
143
7a9e5112 144func UpdateMarketConfig(c *gin.Context) {
145 query := &UpdateMarketConfigQuery{}
146
147 query.In.User = GetUser(c)
148 query.In.Market = c.Param("name")
149 query.In.Key = c.PostForm("key")
150 query.In.Secret = c.PostForm("secret")
151
152 RunQuery(query, c)
153}
85545aba 154
155func PasswordReset(c *gin.Context) {
156 query := &PasswordResetQuery{}
157
158 query.In.Email = c.PostForm("email")
159
160 RunQuery(query, c)
161}
162
163func ChangePassword(c *gin.Context) {
164 query := &ChangePasswordQuery{}
165
166 query.In.Token = c.PostForm("token")
167 query.In.Password = c.PostForm("password")
168
169 RunQuery(query, c)
170}
2da5b12c 171
172func ConfirmEmail(c *gin.Context) {
173 query := &ConfirmEmailQuery{}
174
175 query.In.Token = c.PostForm("token")
176
177 RunQuery(query, c)
178}
d1c0ccfc 179
180func UserAccount(c *gin.Context) {
181 query := &UserAccountQuery{}
182
183 query.In.User = GetUser(c)
184
185 RunQuery(query, c)
186}