diff options
Diffstat (limited to 'api/routes.go')
-rw-r--r-- | api/routes.go | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/api/routes.go b/api/routes.go new file mode 100644 index 0000000..d7e712c --- /dev/null +++ b/api/routes.go | |||
@@ -0,0 +1,123 @@ | |||
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 | }, | ||
29 | }, | ||
30 | { | ||
31 | "/otp", | ||
32 | []Middleware{JwtAuth, UserConfirmed}, | ||
33 | []Route{ | ||
34 | {"GET", []gin.HandlerFunc{OtpEnrollment}, "/enroll"}, | ||
35 | {"POST", []gin.HandlerFunc{OtpValidate}, "/validate"}, | ||
36 | }, | ||
37 | }, | ||
38 | { | ||
39 | "/market", | ||
40 | []Middleware{JwtAuth, UserConfirmed, OtpAuth}, | ||
41 | []Route{ | ||
42 | {"GET", []gin.HandlerFunc{GetMarketConfig}, "/:name"}, | ||
43 | {"POST", []gin.HandlerFunc{UpdateMarketConfig}, "/:name/update"}, | ||
44 | }, | ||
45 | }, | ||
46 | } | ||
47 | |||
48 | func Signup(c *gin.Context) { | ||
49 | query := &SignupQuery{} | ||
50 | |||
51 | query.In.Email = c.PostForm("email") | ||
52 | query.In.Password = c.PostForm("password") | ||
53 | |||
54 | RunQuery(query, c) | ||
55 | } | ||
56 | |||
57 | func Signin(c *gin.Context) { | ||
58 | query := &SigninQuery{} | ||
59 | |||
60 | query.In.Email = c.PostForm("email") | ||
61 | query.In.Password = c.PostForm("password") | ||
62 | |||
63 | RunQuery(query, c) | ||
64 | } | ||
65 | |||
66 | func OtpValidate(c *gin.Context) { | ||
67 | query := &OtpValidateQuery{} | ||
68 | |||
69 | query.In.Pass = c.PostForm("pass") | ||
70 | query.In.User = GetUser(c) | ||
71 | query.In.Claims = GetClaims(c) | ||
72 | |||
73 | RunQuery(query, c) | ||
74 | } | ||
75 | |||
76 | func OtpEnrollment(c *gin.Context) { | ||
77 | query := &OtpEnrollmentQuery{} | ||
78 | |||
79 | query.In.User = GetUser(c) | ||
80 | |||
81 | qrPng, secret, err := query.Run() | ||
82 | if err != nil { | ||
83 | WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c) | ||
84 | c.Error(err) | ||
85 | return | ||
86 | } | ||
87 | |||
88 | if c.Query("format") == "png" { | ||
89 | c.Header("X-OTP-Secret", secret) | ||
90 | WriteBinary("image/png", qrPng.Bytes(), c) | ||
91 | } else { | ||
92 | response := struct { | ||
93 | Base64img string `json:"base64img"` | ||
94 | OtpSecret string `json:"secret"` | ||
95 | }{ | ||
96 | base64.StdEncoding.EncodeToString(qrPng.Bytes()), | ||
97 | secret, | ||
98 | } | ||
99 | |||
100 | WriteJsonResponse(SuccessResponse(response), c) | ||
101 | } | ||
102 | |||
103 | } | ||
104 | |||
105 | func GetMarketConfig(c *gin.Context) { | ||
106 | query := &MarketConfigQuery{} | ||
107 | |||
108 | query.In.User = GetUser(c) | ||
109 | query.In.Market = c.Param("name") | ||
110 | |||
111 | RunQuery(query, c) | ||
112 | } | ||
113 | |||
114 | func UpdateMarketConfig(c *gin.Context) { | ||
115 | query := &UpdateMarketConfigQuery{} | ||
116 | |||
117 | query.In.User = GetUser(c) | ||
118 | query.In.Market = c.Param("name") | ||
119 | query.In.Key = c.PostForm("key") | ||
120 | query.In.Secret = c.PostForm("secret") | ||
121 | |||
122 | RunQuery(query, c) | ||
123 | } | ||