|
| 1 | +/* |
| 2 | + * |
| 3 | + * Created by LoginRadius Development Team |
| 4 | + Copyright 2019 LoginRadius Inc. All rights reserved. |
| 5 | +*/ |
| 6 | + |
| 7 | +package com.loginradius.sdk.api.cloud; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import com.google.gson.Gson; |
| 13 | +import com.google.gson.reflect.TypeToken; |
| 14 | +import com.loginradius.sdk.helper.JsonDeserializer; |
| 15 | +import com.loginradius.sdk.helper.LoginRadiusRequest; |
| 16 | +import com.loginradius.sdk.helper.LoginRadiusValidator; |
| 17 | +import com.loginradius.sdk.models.requestmodels.SsoAuthenticationModel; |
| 18 | +import com.loginradius.sdk.models.responsemodels.SsoJwtResponseData; |
| 19 | +import com.loginradius.sdk.util.AsyncHandler; |
| 20 | +import com.loginradius.sdk.util.ErrorResponse; |
| 21 | +import com.loginradius.sdk.util.LoginRadiusSDK; |
| 22 | + |
| 23 | + |
| 24 | +public class SsoJwtApi { |
| 25 | + private static Gson gson =new Gson(); |
| 26 | + |
| 27 | + public SsoJwtApi(){ |
| 28 | + if (!LoginRadiusSDK.validate()){ |
| 29 | + throw new LoginRadiusSDK.InitializeException(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + // <summary> |
| 36 | + // This API is used to get the JWT token by access token. |
| 37 | + // </summary> |
| 38 | + // <param name="accessToken">Uniquely generated identifier key by LoginRadius that is activated after successful authentication.</param> |
| 39 | + // <param name="jwtAppName">Jwt App Name</param> |
| 40 | + // <returns>Response containing Definition Complete SsoJwtResponseData data</returns> |
| 41 | + |
| 42 | + public void jwtTokenByAccessToken(String accessToken, String jwtAppName,final AsyncHandler<SsoJwtResponseData> handler) { |
| 43 | + |
| 44 | + if (LoginRadiusValidator.isNullOrWhiteSpace(accessToken)) { |
| 45 | + throw new IllegalArgumentException(LoginRadiusValidator.getValidationMessage("accessToken")); |
| 46 | + } |
| 47 | + |
| 48 | + if (LoginRadiusValidator.isNullOrWhiteSpace(jwtAppName)) { |
| 49 | + throw new IllegalArgumentException(LoginRadiusValidator.getValidationMessage("jwtAppName")); |
| 50 | + } |
| 51 | + |
| 52 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 53 | + queryParameters.put("apiKey", LoginRadiusSDK.getApiKey()); |
| 54 | + queryParameters.put("access_token", accessToken); |
| 55 | + queryParameters.put("jwtapp", jwtAppName); |
| 56 | + |
| 57 | + String resourcePath = "sso/jwt/api/token"; |
| 58 | + |
| 59 | + LoginRadiusRequest.execute("GET", resourcePath, queryParameters,null,new AsyncHandler<String>() { |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onSuccess(String response) { |
| 63 | + TypeToken<SsoJwtResponseData> typeToken = new TypeToken<SsoJwtResponseData>() {}; |
| 64 | + SsoJwtResponseData successResponse = JsonDeserializer.deserializeJson(response,typeToken); |
| 65 | + handler.onSuccess(successResponse); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onFailure(ErrorResponse errorResponse) { |
| 70 | + handler.onFailure(errorResponse); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + // <summary> |
| 76 | + // This API is used to get a JWT token by Email and Password. |
| 77 | + // </summary> |
| 78 | + // <param name="ssoAuthenticationModel">Model Class containing Definition of payload for SSO Jwt Cloud Api</param> |
| 79 | + // <param name="accessToken">Uniquely generated identifier key by LoginRadius that is activated after successful authentication.</param> |
| 80 | + // <param name="jwtAppName">Jwt App Name</param> |
| 81 | + // <returns>Response containing Definition Complete SsoJwtResponseData data</returns> |
| 82 | + |
| 83 | + public void jwtTokenByEmail(SsoAuthenticationModel ssoAuthenticationModel, String jwtAppName,String emailTemplate,String loginUrl, String verificationUrl,final AsyncHandler<SsoJwtResponseData> handler) { |
| 84 | + |
| 85 | + if (ssoAuthenticationModel == null) { |
| 86 | + throw new IllegalArgumentException(LoginRadiusValidator.getValidationMessage("ssoAuthenticationModel")); |
| 87 | + } |
| 88 | + if (LoginRadiusValidator.isNullOrWhiteSpace(jwtAppName)) { |
| 89 | + throw new IllegalArgumentException(LoginRadiusValidator.getValidationMessage("jwtAppName")); |
| 90 | + } |
| 91 | + |
| 92 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 93 | + queryParameters.put("apiKey", LoginRadiusSDK.getApiKey()); |
| 94 | + queryParameters.put("jwtapp", jwtAppName); |
| 95 | + |
| 96 | + if (!LoginRadiusValidator.isNullOrWhiteSpace(emailTemplate)) { |
| 97 | + queryParameters.put("emailTemplate", emailTemplate); |
| 98 | + } |
| 99 | + |
| 100 | + if (!LoginRadiusValidator.isNullOrWhiteSpace(loginUrl)) { |
| 101 | + queryParameters.put("loginUrl", loginUrl); |
| 102 | + } |
| 103 | + |
| 104 | + if (!LoginRadiusValidator.isNullOrWhiteSpace(verificationUrl)) { |
| 105 | + queryParameters.put("verificationurl", verificationUrl); |
| 106 | + } |
| 107 | + String resourcePath = "sso/jwt/api/login"; |
| 108 | + |
| 109 | + LoginRadiusRequest.execute("POST", resourcePath, queryParameters,gson.toJson(ssoAuthenticationModel),new AsyncHandler<String>() { |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onSuccess(String response) { |
| 113 | + TypeToken<SsoJwtResponseData> typeToken = new TypeToken<SsoJwtResponseData>() {}; |
| 114 | + SsoJwtResponseData successResponse = JsonDeserializer.deserializeJson(response,typeToken); |
| 115 | + handler.onSuccess(successResponse); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onFailure(ErrorResponse errorResponse) { |
| 120 | + handler.onFailure(errorResponse); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + // <summary> |
| 126 | + // This API is used to get a JWT token by UserName and Password. |
| 127 | + // </summary> |
| 128 | + // <param name="ssoAuthenticationModel">Model Class containing Definition of payload for SSO Jwt Cloud Api</param> |
| 129 | + // <param name="accessToken">Uniquely generated identifier key by LoginRadius that is activated after successful authentication.</param> |
| 130 | + // <param name="jwtAppName">Jwt App Name</param> |
| 131 | + // <returns>Response containing Definition Complete SsoJwtResponseData data</returns> |
| 132 | + |
| 133 | + public void jwtTokenByUserName(SsoAuthenticationModel ssoAuthenticationModel, String jwtAppName, String emailTemplate,String loginUrl, String verificationUrl, final AsyncHandler<SsoJwtResponseData> handler) { |
| 134 | + |
| 135 | + |
| 136 | + if (ssoAuthenticationModel == null) { |
| 137 | + throw new IllegalArgumentException(LoginRadiusValidator.getValidationMessage("ssoAuthenticationModel")); |
| 138 | + } |
| 139 | + |
| 140 | + if (LoginRadiusValidator.isNullOrWhiteSpace(jwtAppName)) { |
| 141 | + throw new IllegalArgumentException(LoginRadiusValidator.getValidationMessage("jwtAppName")); |
| 142 | + } |
| 143 | + |
| 144 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 145 | + queryParameters.put("apiKey", LoginRadiusSDK.getApiKey()); |
| 146 | + queryParameters.put("jwtapp", jwtAppName); |
| 147 | + |
| 148 | + if (!LoginRadiusValidator.isNullOrWhiteSpace(emailTemplate)) { |
| 149 | + queryParameters.put("emailTemplate", emailTemplate); |
| 150 | + } |
| 151 | + |
| 152 | + if (!LoginRadiusValidator.isNullOrWhiteSpace(loginUrl)) { |
| 153 | + queryParameters.put("loginUrl", loginUrl); |
| 154 | + } |
| 155 | + |
| 156 | + if (!LoginRadiusValidator.isNullOrWhiteSpace(verificationUrl)) { |
| 157 | + queryParameters.put("verificationurl", verificationUrl); |
| 158 | + } |
| 159 | + String resourcePath = "sso/jwt/api/login"; |
| 160 | + |
| 161 | + LoginRadiusRequest.execute("POST", resourcePath, queryParameters,gson.toJson(ssoAuthenticationModel),new AsyncHandler<String>() { |
| 162 | + |
| 163 | + @Override |
| 164 | + public void onSuccess(String response) { |
| 165 | + TypeToken<SsoJwtResponseData> typeToken = new TypeToken<SsoJwtResponseData>() {}; |
| 166 | + SsoJwtResponseData successResponse = JsonDeserializer.deserializeJson(response,typeToken); |
| 167 | + handler.onSuccess(successResponse); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void onFailure(ErrorResponse errorResponse) { |
| 172 | + handler.onFailure(errorResponse); |
| 173 | + } |
| 174 | + }); |
| 175 | + } |
| 176 | + // <summary> |
| 177 | + // This API is used to get a JWT token by Phone and Password. |
| 178 | + // </summary> |
| 179 | + // <param name="ssoAuthenticationModel">Model Class containing Definition of payload for SSO Jwt Cloud Api</param> |
| 180 | + // <param name="accessToken">Uniquely generated identifier key by LoginRadius that is activated after successful authentication.</param> |
| 181 | + // <param name="jwtAppName">Jwt App Name</param> |
| 182 | + // <returns>Response containing Definition Complete SsoJwtResponseData data</returns> |
| 183 | + |
| 184 | + public void jwtTokenByPhone(SsoAuthenticationModel ssoAuthenticationModel,String jwtAppName,String emailTemplate,String loginUrl, String verificationUrl,final AsyncHandler<SsoJwtResponseData> handler) { |
| 185 | + |
| 186 | + if (ssoAuthenticationModel == null) { |
| 187 | + throw new IllegalArgumentException(LoginRadiusValidator.getValidationMessage("ssoAuthenticationModel")); |
| 188 | + } |
| 189 | + |
| 190 | + if (LoginRadiusValidator.isNullOrWhiteSpace(jwtAppName)) { |
| 191 | + throw new IllegalArgumentException(LoginRadiusValidator.getValidationMessage("jwtAppName")); |
| 192 | + } |
| 193 | + |
| 194 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 195 | + queryParameters.put("apiKey", LoginRadiusSDK.getApiKey()); |
| 196 | + queryParameters.put("jwtapp", jwtAppName); |
| 197 | + |
| 198 | + |
| 199 | + if (!LoginRadiusValidator.isNullOrWhiteSpace(emailTemplate)) { |
| 200 | + queryParameters.put("emailTemplate", emailTemplate); |
| 201 | + } |
| 202 | + |
| 203 | + if (!LoginRadiusValidator.isNullOrWhiteSpace(loginUrl)) { |
| 204 | + queryParameters.put("loginUrl", loginUrl); |
| 205 | + } |
| 206 | + |
| 207 | + if (!LoginRadiusValidator.isNullOrWhiteSpace(verificationUrl)) { |
| 208 | + queryParameters.put("verificationurl", verificationUrl); |
| 209 | + } |
| 210 | + |
| 211 | + String resourcePath = "sso/jwt/api/login"; |
| 212 | + |
| 213 | + LoginRadiusRequest.execute("POST", resourcePath, queryParameters,gson.toJson(ssoAuthenticationModel),new AsyncHandler<String>() { |
| 214 | + |
| 215 | + @Override |
| 216 | + public void onSuccess(String response) { |
| 217 | + TypeToken<SsoJwtResponseData> typeToken = new TypeToken<SsoJwtResponseData>() {}; |
| 218 | + SsoJwtResponseData successResponse = JsonDeserializer.deserializeJson(response,typeToken); |
| 219 | + handler.onSuccess(successResponse); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public void onFailure(ErrorResponse errorResponse) { |
| 224 | + handler.onFailure(errorResponse); |
| 225 | + } |
| 226 | + }); |
| 227 | + } |
| 228 | + |
| 229 | + |
| 230 | +} |
0 commit comments