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