protected override Java.Lang.Void RunInBackground(params Java.Lang.Void[] @params) { try { HttpPost post = new HttpPost("https://layer-identity-provider.herokuapp" + ".com/identity_tokens"); post.SetHeader("Content-Type", "application/json"); post.SetHeader("Accept", "application/json"); JSONObject json = new JSONObject() .Put("app_id", mClient.AppId) .Put("user_id", mUserId) .Put("nonce", mNonce); post.Entity = new StringEntity(json.ToString()); IHttpResponse response = (new DefaultHttpClient()).Execute(post); string eit = (new JSONObject(EntityUtils.ToString(response.Entity))) .OptString("identity_token"); mClient.AnswerAuthenticationChallenge(eit); } catch (Exception e) { Console.WriteLine(e.ToString()); } return(null); }
protected override Java.Lang.Void RunInBackground(params Java.Lang.Void[] @params) { try { HttpPost post = new HttpPost("https://layer-identity-provider.herokuapp.com/identity_tokens"); post.SetHeader("Content-Type", "application/json"); post.SetHeader("Accept", "application/json"); JSONObject json = new JSONObject() .Put("app_id", _layerClient.AppId) .Put("user_id", _userId) .Put("nonce", _nonce); post.Entity = new StringEntity(json.ToString()); IHttpResponse response = (new DefaultHttpClient()).Execute(post); string eit = (new JSONObject(EntityUtils.ToString(response.Entity))) .OptString("identity_token"); /* * 3. Submit identity token to Layer for validation */ _layerClient.AnswerAuthenticationChallenge(eit); } catch (Exception ex) { Log.Debug(nameof(MyAuthenticationListener), ex.StackTrace); } return(null); }
private void RespondToChallenge(LayerClient layerClient, string nonce) { Credentials credentials = new Credentials(mPreferences.GetString("appId", null), mPreferences.GetString("name", null)); if (credentials.GetUserName() == null || credentials.GetLayerAppId() == null) { if (Log.IsLoggable(Log.WARN)) { Log.w("No stored credentials to respond to challenge with"); } return; } try { // Post request string url = "https://layer-identity-provider.herokuapp.com/apps/" + credentials.GetLayerAppId() + "/atlas_identities"; HttpURLConnection connection = (HttpURLConnection) new URL(url).OpenConnection(); connection.DoInput = true; connection.DoOutput = true; connection.RequestMethod = "POST"; connection.SetRequestProperty("Content-Type", "application/json"); connection.SetRequestProperty("Accept", "application/json"); connection.SetRequestProperty("X_LAYER_APP_ID", credentials.GetLayerAppId()); // Credentials JSONObject rootObject = new JSONObject() .Put("nonce", nonce) .Put("name", credentials.GetUserName()); connection.SetRequestProperty("Content-Type", "application/json; charset=UTF-8"); Stream os = connection.OutputStream; var bytes = Encoding.UTF8.GetBytes(rootObject.ToString()); os.Write(bytes, 0, bytes.Length); os.Close(); // Handle failure HttpStatus statusCode = connection.ResponseCode; if (statusCode != HttpStatus.Ok && statusCode != HttpStatus.Created) { string error = string.Format("Got status %d when requesting authentication for '%s' with nonce '%s' from '%s'", statusCode, credentials.GetUserName(), nonce, url); if (Log.IsLoggable(Log.ERROR)) { Log.e(error); } if (mCallback != null) { mCallback.OnError(this, error); } return; } // Parse response Stream input = connection.InputStream; string result = CUtil.StreamToString(input); input.Close(); connection.Disconnect(); JSONObject json = new JSONObject(result); if (json.Has("error")) { string error = json.GetString("error"); if (Log.IsLoggable(Log.ERROR)) { Log.e(error); } if (mCallback != null) { mCallback.OnError(this, error); } return; } // Answer authentication challenge. string identityToken = json.OptString("identity_token", null); if (Log.IsLoggable(Log.VERBOSE)) { Log.v("Got identity token: " + identityToken); } layerClient.AnswerAuthenticationChallenge(identityToken); } catch (Exception e) { string error = "Error when authenticating with provider: " + e.Message; if (Log.IsLoggable(Log.ERROR)) { Log.e(error, e); } if (mCallback != null) { mCallback.OnError(this, error); } } }