具有SAML(O365)的Android Cognito

时间:2018-10-16 11:35:50

标签: android amazon-web-services amazon saml amazon-cognito

我正在尝试通过Android应用程序将SAML与Amazon Cognito集成。我跟随101apps.co.za观看了一系列YouTube视频,在那里我能够通过托管的UI(使用他们在其文档中提供的链接格式)以及通过我的简单android应用程序在Cognito控制台上成功验证手动创建的用户。

在我的情况下,由于这是公司环境,因此应该使用Office 365电子邮件ID和密码对用户进行身份验证。 我确实遵循了一篇文章,我设法成功使用Office 365登录。 链接-AWS Cognito With Federation To Office 365 我能够使用Cognito生成的UI轻松登录。请参考下图

Sign In UI

唯一的问题是我无法在Android应用程序上使用它。以下是我得到的错误。

Login failed User does not exist. (Service: AmazonCognitoIdentityProvider; Status Code: 400; Error Code: UserNotFoundException; Request ID: 3cd1e93e-d118-11e8-af77-098f4bda99f1)

下面是我简单的Android应用程序中的代码,该代码具有2个用于输入用户名,密码和登录按钮的文本框。这是按照我之前提到的YouTube教程系列实现的。

package com.midassafety.chulankumara.googleauthentication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler;

import org.w3c.dom.Text;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "Cognito";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final EditText editTextUsername = findViewById(R.id.username);
        final EditText editTextPassword = findViewById(R.id.password);

        final AuthenticationHandler authenticationHandler = new AuthenticationHandler() {
            @Override
            public void onSuccess(CognitoUserSession cognitoUserSession, CognitoDevice cognitoDevice) {
                Log.i(TAG, "Login successfull, can get tokens here");
                cognitoUserSession.getIdToken();
            }

            @Override
            public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
                Log.i(TAG, "Login getAuthenticationDetails()...");

                /*need to get the userId & password to continue*/
                AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, String.valueOf(editTextPassword.getText()), null);

                // Pass the user sign-in credentials to the continuation
                authenticationContinuation.setAuthenticationDetails(authenticationDetails);

                // Allow this sign-in to continue
                authenticationContinuation.continueTask();
            }

            @Override
            public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) {
                Log.i(TAG, "Login getMFACode()...");
            }

            @Override
            public void authenticationChallenge(ChallengeContinuation challengeContinuation) {
                Log.i(TAG, "Login authenticationChallenge()...");
            }

            @Override
            public void onFailure(Exception e) {
                Log.i(TAG,"Login failed " + e.getLocalizedMessage());
            }
        };

        Button buttonLogin = findViewById(R.id.login);
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CognitoSettings cognitoSettings = new CognitoSettings(LoginActivity.this);

                CognitoUser thisUser = cognitoSettings.getUserPool().getUser(String.valueOf(editTextUsername.getText()));

                Log.i(TAG,"Login button clicked....");

                thisUser.getSessionInBackground(authenticationHandler);
            }
        });
    }
}

这是CognitoSettings.java类,用于处理基本设置

package com.midassafety.chulankumara.googleauthentication;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool;
import com.amazonaws.regions.Regions;

public class CognitoSettings  {
    private String userPoolId = "ap-southeast-1_######";
    private String clientId = "#############";
    private String clientSecret = "************************redacted********************";
    private Regions cognitoRegion = Regions.AP_SOUTHEAST_1;

    private Context context;

    public CognitoSettings(Context context){
        this.context = context;
    }

    public String getUserPoolId(){
        return userPoolId;
    }

    public String getClientId(){
        return clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public Regions getCognitoRegion() {
        return cognitoRegion;
    }

    public CognitoUserPool getUserPool(){
        return new CognitoUserPool(context, userPoolId, clientId, clientSecret, cognitoRegion);
    }
}

我检查UserPool,并查看我创建的SAML身份提供程序的“自动生成”组。

Autogenerated Groups

我整天都在扫荡互联网,但是没有运气。

我在做错什么,或者我应该做些什么来验证使用SAML和O365的用户的身份?

请帮助!...

0 个答案:

没有答案