LoginFromJSONReader performs authentication given a login request body reader and some context. It returns the basic login information and a cleanup function to be called after authorization has completed, with the result of the authorization. If the final return value is non-nil, an error occurred
(ctx context.Context, r io.Reader, useraccountAPI uapi.UserLoginAPI, userAPI UserInternalAPIForLogin, cfg *config.ClientAPI, args ...interface{})
| 33 | // If the final return value is non-nil, an error occurred and the cleanup function |
| 34 | // is nil. |
| 35 | func LoginFromJSONReader(ctx context.Context, r io.Reader, useraccountAPI uapi.UserLoginAPI, userAPI UserInternalAPIForLogin, cfg *config.ClientAPI, args ...interface{}) (*Login, LoginCleanupFunc, *util.JSONResponse) { |
| 36 | reqBytes, err := io.ReadAll(r) |
| 37 | if err != nil { |
| 38 | err := &util.JSONResponse{ |
| 39 | Code: http.StatusBadRequest, |
| 40 | JSON: jsonerror.BadJSON("Reading request body failed: " + err.Error()), |
| 41 | } |
| 42 | return nil, nil, err |
| 43 | } |
| 44 | |
| 45 | var header struct { |
| 46 | Type string `json:"type"` |
| 47 | } |
| 48 | if err := json.Unmarshal(reqBytes, &header); err != nil { |
| 49 | err := &util.JSONResponse{ |
| 50 | Code: http.StatusBadRequest, |
| 51 | JSON: jsonerror.BadJSON("Reading request body failed: " + err.Error()), |
| 52 | } |
| 53 | return nil, nil, err |
| 54 | } |
| 55 | |
| 56 | var typ Type |
| 57 | switch header.Type { |
| 58 | case authtypes.LoginTypePassword: |
| 59 | typ = &LoginTypePassword{ |
| 60 | GetAccountByPassword: useraccountAPI.QueryAccountByPassword, |
| 61 | Config: cfg, |
| 62 | } |
| 63 | case authtypes.LoginTypeToken: |
| 64 | typ = &LoginTypeToken{ |
| 65 | UserAPI: userAPI, |
| 66 | Config: cfg, |
| 67 | } |
| 68 | case authtypes.LoginTypeXs: |
| 69 | typ = &LoginTypeXs{ |
| 70 | UserAPI: userAPI, |
| 71 | Config: cfg, |
| 72 | } |
| 73 | case authtypes.LoginTypeCosmos: |
| 74 | if len(args) == 0 { |
| 75 | err := util.JSONResponse{ |
| 76 | Code: http.StatusBadRequest, |
| 77 | JSON: jsonerror.MissingParam("missing clientUserApi"), |
| 78 | } |
| 79 | return nil, nil, &err |
| 80 | } |
| 81 | typ = &LoginTypeCosmos{ |
| 82 | UserAPI: args[0].(uapi.ClientUserAPI), |
| 83 | Config: cfg, |
| 84 | } |
| 85 | case authtypes.LoginTypeJwt: |
| 86 | if len(args) == 0 { |
| 87 | err := util.JSONResponse{ |
| 88 | Code: http.StatusBadRequest, |
| 89 | JSON: jsonerror.MissingParam("missing clientUserApi"), |
| 90 | } |
| 91 | return nil, nil, &err |
| 92 | } |