gin_handler.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package zllauth_gin
  2. import (
  3. "git.swzry.com/zry/zllauth1/zllauth1"
  4. "github.com/gin-gonic/gin"
  5. "time"
  6. )
  7. const API_VER = "0.0.1"
  8. type ZLLAuthGinHandler struct {
  9. gingrp *gin.RouterGroup
  10. zllhdl *zllauth1.ZLLAuthHandler
  11. elh EventLogHandler
  12. }
  13. func NewZLLAuthGinHandler(gingrp *gin.RouterGroup, config zllauth1.ZLLAuthConfig) *ZLLAuthGinHandler {
  14. o := &ZLLAuthGinHandler{
  15. gingrp: gingrp,
  16. zllhdl: zllauth1.NewZLLAuthHandler(config),
  17. }
  18. o.gingrp.GET("/", o.wh_Home)
  19. o.gingrp.GET("/get_encrypt_info.maki", o.wh_GetEncryptInfo)
  20. o.gingrp.POST("/login.maki", o.wh_Login)
  21. o.gingrp.POST("/renew_jwt.maki", o.wh_RenewJWT)
  22. return o
  23. }
  24. func (h *ZLLAuthGinHandler) InitSecure() error {
  25. return h.zllhdl.InitSecure()
  26. }
  27. func (h *ZLLAuthGinHandler) SetEventLogHandler(elh EventLogHandler) {
  28. h.elh = elh
  29. }
  30. func (h *ZLLAuthGinHandler) wh_Home(ctx *gin.Context) {
  31. ctx.JSON(200, gin.H{
  32. "suc": true,
  33. "api_name": "zllauth1_gin_api",
  34. "zllauth_ver": "1.0.0",
  35. "api_ver": API_VER,
  36. "time": time.Now(),
  37. "api_url": gin.H{
  38. "get_encrypt_info": gin.H{
  39. "rel_url": "/get_encrypt_info.maki",
  40. "method": "GET",
  41. },
  42. "login": gin.H{
  43. "rel_url": "/login.maki",
  44. "method": "POST",
  45. "post_data_mime": "application/json",
  46. "usage": gin.H{
  47. "login_data": "<sm2 encrypted data>",
  48. },
  49. },
  50. "renew_jwt": gin.H{
  51. "rel_url": "/renew_jwt.maki",
  52. "method": "POST",
  53. "post_data_mime": "application/json",
  54. "usage": gin.H{
  55. "old_jwt": "<old jwt string>",
  56. },
  57. },
  58. },
  59. })
  60. }
  61. func (h *ZLLAuthGinHandler) wh_GetEncryptInfo(ctx *gin.Context) {
  62. ei, err := h.zllhdl.GetEncryptionInfo()
  63. if err != nil {
  64. if h.elh != nil {
  65. h.elh.InternalError("get_encrypt_info", ctx.Request, err)
  66. }
  67. ctx.JSON(200, gin.H{
  68. "suc": false,
  69. "err_hcode": 502,
  70. "err_ecode": 1,
  71. "err_msg": "internal server error",
  72. })
  73. return
  74. }
  75. ctx.JSON(200, gin.H{
  76. "api_ver": API_VER,
  77. "encrypt_info": ei,
  78. })
  79. }
  80. func (h *ZLLAuthGinHandler) wh_Login(ctx *gin.Context) {
  81. var jdata JsonDef_LoginArugument
  82. err := ctx.BindJSON(&jdata)
  83. if err != nil {
  84. if h.elh != nil {
  85. h.elh.JsonDecodeError("login", ctx.Request, err)
  86. }
  87. ctx.JSON(200, gin.H{
  88. "suc": false,
  89. "err_hcode": 400,
  90. "err_ecode": 2,
  91. "err_msg": "invalid arguments",
  92. })
  93. return
  94. }
  95. sei := map[string]interface{}{
  96. "client_ip": ctx.Request.RemoteAddr,
  97. }
  98. se, jwtdata, ve, err := h.zllhdl.HandlingLogin(jdata.LoginData, sei)
  99. if se {
  100. ctx.JSON(200, gin.H{
  101. "suc": true,
  102. "login_suc": true,
  103. "jwt": gin.H{
  104. "token": jwtdata.JwtStr,
  105. "issue_time": jwtdata.IssueTime,
  106. "expire_time": jwtdata.ExpireTime,
  107. "ttl": float64(h.zllhdl.GetJwtTTL()) / 1000_000_000,
  108. },
  109. })
  110. return
  111. }
  112. if h.elh != nil {
  113. h.elh.LoginFailed(ctx.Request, ve, err)
  114. }
  115. switch ve {
  116. case zllauth1.LFT_INVALID_USERNAME_OR_PASSWORD:
  117. {
  118. ctx.JSON(200, gin.H{
  119. "suc": true,
  120. "login_suc": false,
  121. "login_ecode": "invalid_username_or_password",
  122. })
  123. return
  124. }
  125. case zllauth1.LFT_PERMISSION_DENIED:
  126. {
  127. ctx.JSON(200, gin.H{
  128. "suc": true,
  129. "login_suc": false,
  130. "login_ecode": "permission_denied",
  131. })
  132. return
  133. }
  134. case zllauth1.LFT_USER_BANNED:
  135. {
  136. ctx.JSON(200, gin.H{
  137. "suc": true,
  138. "login_suc": false,
  139. "login_ecode": "user_banned",
  140. })
  141. return
  142. }
  143. case zllauth1.LFT_DECODE_HEX_FAIL:
  144. {
  145. ctx.JSON(200, gin.H{
  146. "suc": true,
  147. "login_suc": false,
  148. "login_ecode": "argument_transport_error",
  149. "external_info": "decode hex failed",
  150. })
  151. return
  152. }
  153. case zllauth1.LFT_DECRYPT_SM2_FAIL:
  154. {
  155. ctx.JSON(200, gin.H{
  156. "suc": true,
  157. "login_suc": false,
  158. "login_ecode": "argument_transport_error",
  159. "external_info": "decrypt sm2 failed",
  160. })
  161. return
  162. }
  163. case zllauth1.LFT_LOGIN_INFO_FIELDS_NOT_MATCH:
  164. {
  165. ctx.JSON(200, gin.H{
  166. "suc": true,
  167. "login_suc": false,
  168. "login_ecode": "login_info_fields_not_match",
  169. "external_info": "login info fields not match",
  170. })
  171. return
  172. }
  173. default:
  174. {
  175. ctx.JSON(200, gin.H{
  176. "suc": false,
  177. "err_hcode": 502,
  178. "err_ecode": 3,
  179. "err_msg": "internal server error",
  180. })
  181. return
  182. }
  183. }
  184. }
  185. type HandlingOtherEncryptedRequestNextFunc func(returnData interface{})
  186. func (h *ZLLAuthGinHandler) HandlingOtherEncryptedRequest(ctx *gin.Context, v interface{}) (isOK bool, next HandlingOtherEncryptedRequestNextFunc) {
  187. isOK = false
  188. next = nil
  189. var jdata JsonDef_LoginArugument
  190. err := ctx.BindJSON(&jdata)
  191. if err != nil {
  192. if h.elh != nil {
  193. h.elh.JsonDecodeError("oerutil", ctx.Request, err)
  194. }
  195. ctx.JSON(200, gin.H{
  196. "suc": false,
  197. "err_hcode": 400,
  198. "err_ecode": 2,
  199. "err_msg": "invalid arguments",
  200. })
  201. return
  202. }
  203. ve, ie := h.zllhdl.HandlingOtherEncryptedRequest(jdata.LoginData, v)
  204. if h.elh != nil {
  205. h.elh.HandlingOtherEncryptedRequestFailed(ctx.Request, ve, ie)
  206. }
  207. if ve == zllauth1.LFT_SUCCESS {
  208. isOK = true
  209. next = func(returnData interface{}) {
  210. ctx.JSON(200, gin.H{
  211. "suc": true,
  212. "data": returnData,
  213. })
  214. }
  215. return
  216. }
  217. switch ve {
  218. case zllauth1.LFT_DECODE_HEX_FAIL:
  219. {
  220. ctx.JSON(200, gin.H{
  221. "suc": true,
  222. "login_suc": false,
  223. "login_ecode": "argument_transport_error",
  224. "external_info": "decode hex failed",
  225. })
  226. return
  227. }
  228. case zllauth1.LFT_DECRYPT_SM2_FAIL:
  229. {
  230. ctx.JSON(200, gin.H{
  231. "suc": true,
  232. "login_suc": false,
  233. "login_ecode": "argument_transport_error",
  234. "external_info": "decrypt sm2 failed",
  235. })
  236. return
  237. }
  238. default:
  239. {
  240. ctx.JSON(200, gin.H{
  241. "suc": false,
  242. "err_hcode": 502,
  243. "err_ecode": 3,
  244. "err_msg": "internal server error",
  245. })
  246. return
  247. }
  248. }
  249. }
  250. func (h *ZLLAuthGinHandler) CheckJWT(jwtstr string) (ok bool, extData map[string]interface{}) {
  251. se, exi, _, _ := h.zllhdl.CheckJWT(jwtstr)
  252. if se {
  253. return true, exi
  254. }
  255. return false, nil
  256. }
  257. func (h *ZLLAuthGinHandler) wh_RenewJWT(ctx *gin.Context) {
  258. var jdata JsonDef_RenewJWT
  259. err := ctx.BindJSON(&jdata)
  260. if err != nil {
  261. if h.elh != nil {
  262. h.elh.JsonDecodeError("login", ctx.Request, err)
  263. }
  264. ctx.JSON(200, gin.H{
  265. "suc": false,
  266. "err_hcode": 400,
  267. "err_ecode": 2,
  268. "err_msg": "invalid arguments",
  269. })
  270. return
  271. }
  272. se, njwt, ve, err := h.zllhdl.RenewJWT(jdata.OldJWT)
  273. if se {
  274. ctx.JSON(200, gin.H{
  275. "suc": true,
  276. "renew_suc": true,
  277. "jwt": gin.H{
  278. "token": njwt.JwtStr,
  279. "issue_time": njwt.IssueTime,
  280. "expire_time": njwt.ExpireTime,
  281. "ttl": float64(h.zllhdl.GetJwtTTL()) / 1000_000_000,
  282. },
  283. })
  284. return
  285. }
  286. if h.elh != nil {
  287. h.elh.RenewJWTFailed(ctx.Request, ve, err)
  288. }
  289. switch ve {
  290. case zllauth1.LFT_JWT_CHECK_FAIL:
  291. {
  292. ctx.JSON(200, gin.H{
  293. "suc": true,
  294. "renew_suc": false,
  295. "renew_ecode": "jwt_check_fail",
  296. })
  297. return
  298. }
  299. case zllauth1.LFT_JWT_SUBJECT_NOT_MATCH:
  300. {
  301. ctx.JSON(200, gin.H{
  302. "suc": true,
  303. "renew_suc": false,
  304. "renew_ecode": "jwt_subject_not_match",
  305. })
  306. return
  307. }
  308. case zllauth1.LFT_JWT_ISSUER_NOT_MATCH:
  309. {
  310. ctx.JSON(200, gin.H{
  311. "suc": true,
  312. "renew_suc": false,
  313. "renew_ecode": "jwt_issuer_not_match",
  314. })
  315. return
  316. }
  317. case zllauth1.LFT_JWT_SIGN_ERROR:
  318. {
  319. ctx.JSON(200, gin.H{
  320. "suc": true,
  321. "renew_suc": false,
  322. "renew_ecode": "jwt_sign_fail",
  323. })
  324. return
  325. }
  326. default:
  327. {
  328. ctx.JSON(200, gin.H{
  329. "suc": true,
  330. "renew_suc": false,
  331. "renew_ecode": "internal_error",
  332. })
  333. return
  334. }
  335. }
  336. }