]>
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 | ||
60 | func 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 | ||
69 | func 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 | ||
78 | func 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 | ||
88 | func 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 | ||
117 | func 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 | ||
126 | func 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 | ||
135 | func GetPortfolio(c *gin.Context) { | |
136 | query := &GetPortfolioQuery{} | |
137 | ||
138 | query.In.User = GetUser(c) | |
139 | query.In.Market = c.Param("name") | |
140 | ||
141 | RunQuery(query, c) | |
142 | } | |
143 | ||
144 | func 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 | } | |
154 | ||
155 | func PasswordReset(c *gin.Context) { | |
156 | query := &PasswordResetQuery{} | |
157 | ||
158 | query.In.Email = c.PostForm("email") | |
159 | ||
160 | RunQuery(query, c) | |
161 | } | |
162 | ||
163 | func 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 | } | |
171 | ||
172 | func ConfirmEmail(c *gin.Context) { | |
173 | query := &ConfirmEmailQuery{} | |
174 | ||
175 | query.In.Token = c.PostForm("token") | |
176 | ||
177 | RunQuery(query, c) | |
178 | } | |
179 | ||
180 | func UserAccount(c *gin.Context) { | |
181 | query := &UserAccountQuery{} | |
182 | ||
183 | query.In.User = GetUser(c) | |
184 | ||
185 | RunQuery(query, c) | |
186 | } |