]>
Commit | Line | Data |
---|---|---|
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 | "/user", | |
53 | []Middleware{JwtAuth, UserConfirmed, OtpAuth}, | |
54 | []Route{ | |
55 | {"GET", []gin.HandlerFunc{UserAccount}, "/account"}, | |
56 | }, | |
57 | }, | |
58 | { | |
59 | "/admin", | |
60 | []Middleware{JwtAuth, UserConfirmed, UserIsAdmin, OtpAuth}, | |
61 | []Route{}, | |
62 | }, | |
63 | } | |
64 | ||
65 | func 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 | ||
74 | func 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 | ||
83 | func 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 | ||
93 | func 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 | ||
122 | func 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 | ||
131 | func 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 | ||
140 | func GetPortfolio(c *gin.Context) { | |
141 | query := &GetPortfolioQuery{} | |
142 | ||
143 | query.In.User = GetUser(c) | |
144 | query.In.Market = c.Param("name") | |
145 | ||
146 | RunQuery(query, c) | |
147 | } | |
148 | ||
149 | func 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 | } | |
159 | ||
160 | func PasswordReset(c *gin.Context) { | |
161 | query := &PasswordResetQuery{} | |
162 | ||
163 | query.In.Email = c.PostForm("email") | |
164 | ||
165 | RunQuery(query, c) | |
166 | } | |
167 | ||
168 | func 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 | } | |
176 | ||
177 | func ConfirmEmail(c *gin.Context) { | |
178 | query := &ConfirmEmailQuery{} | |
179 | ||
180 | query.In.Token = c.PostForm("token") | |
181 | ||
182 | RunQuery(query, c) | |
183 | } | |
184 | ||
185 | func UserAccount(c *gin.Context) { | |
186 | query := &UserAccountQuery{} | |
187 | ||
188 | query.In.User = GetUser(c) | |
189 | ||
190 | RunQuery(query, c) | |
191 | } |