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