]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blob - api/routes.go
d0e8cec151dbd550e2c406f06a7498747c9d1207
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / routes.go
1 package api
2
3 import (
4 "encoding/base64"
5
6 "github.com/gin-gonic/gin"
7 )
8
9 type Route struct {
10 Method string
11 Handlers []gin.HandlerFunc
12 Path string
13 }
14
15 type Group struct {
16 Root string
17 Middlewares []Middleware
18 Routes []Route
19 }
20
21 var Groups = []Group{
22 {
23 "",
24 nil,
25 []Route{
26 {"POST", []gin.HandlerFunc{Signup}, "/signup"},
27 {"POST", []gin.HandlerFunc{Signin}, "/signin"},
28 {"POST", []gin.HandlerFunc{PasswordReset}, "/passwordreset"},
29 {"POST", []gin.HandlerFunc{ChangePassword}, "/changepassword"},
30 {"POST", []gin.HandlerFunc{ConfirmEmail}, "/confirmemail"},
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"},
47 {"GET", []gin.HandlerFunc{TestMarketCredentials}, "/:name/test-credentials"},
48 {"GET", []gin.HandlerFunc{GetPortfolio}, "/:name/portfolio"},
49 },
50 },
51 }
52
53 func 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
62 func 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
71 func 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
81 func 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
110 func 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
119 func 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
128 func GetPortfolio(c *gin.Context) {
129 query := &GetPortfolioQuery{}
130
131 query.In.User = GetUser(c)
132 query.In.Market = c.Param("name")
133
134 RunQuery(query, c)
135 }
136
137 func 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 }
147
148 func PasswordReset(c *gin.Context) {
149 query := &PasswordResetQuery{}
150
151 query.In.Email = c.PostForm("email")
152
153 RunQuery(query, c)
154 }
155
156 func 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 }
164
165 func ConfirmEmail(c *gin.Context) {
166 query := &ConfirmEmailQuery{}
167
168 query.In.Token = c.PostForm("token")
169
170 RunQuery(query, c)
171 }