2016-01-18 50 views
3

对Okta和Android没有太多了解。有谁知道一个很好的教程,它展示了如何将Android应用程序连接到Okta框架。或者,我是否实施了SAML SSO实施,然后Okta与此相关联?任何代码示例都会受到赞赏 - 尤其是显示Android通用SSO实现的代码示例,如果存在这样的情况。Android SSO Okta集成示例

+0

对此有任何更新 –

+0

其实,是的。我会试着花几分钟时间来将几个小时内所做的一些代码示例。还需要一些服务器端的工作,我没有这样做(我认为这种差异很大)。 –

+0

感谢一位伙伴,期待更新 –

回答

6

好吧,很多地面覆盖在这里和我没有做的一些工作。但基本思想是,在服务器端(我们使用.Net),我们使用“kentor”创建了SAML通信层。我没有与此工作,但想法是软件沟通的客户端身份提供商(IDP)的SSO(Okta例如)。 IDP客户端通常必须提供具有安全信息且最终为URL的XML元数据,并且向他们提供SSO XML元数据(对不起,我没有在这部分工作!)。

基本上从那里它是非常直接在Android的一面。最重要的是,上述交互产生了一个由SSO客户端提供的URL,您将在Android端使用它创建一个webview,这将允许他们输入他们的登录信息进行验证。

因为我们专门为客户创建了一个白色标签产品(您将在下面看到Constants.SINGLE_SIGNON_URL),但是没有什么能够阻止您在客户通过组织代码进行SSO后将URL返回给客户在(我们现在正在努力)。换句话说,您存储URL或根据哪个客户生成URL,然后在设备向您传递组织代码时返回该URL。该URL实际上是您的服务器,它重定向到SSO的IDP(Okta)登录页面。这是因为OKTA的响应需要到您的服务器,最终它将通过重定向发回到您的webview。我们使用cookies来存储所产生的用户名,以允许正常的登录过程。可能有很多不同的方式来做到这一点,Okta甚至提供本地移动设备功能,但客户必须支持。

这里是一个图,希望能拼写出这些高水平件:

enter image description here

该代码仅覆盖1),2)和5)上面的图所示。 1)对WebView的调用非常明显。 2)实际上是打到您的服务器的Constants.SINGLE_SIGNON_URL的调用,它应该重定向到IDP页面。当用户在那里登录时,它会被发送回服务(SP)并被重定向回WebView。再次,我们在cookie中存储了一些内容,以便继续我们的正常登录。

一个关键是要认识到多次调用WebView的shouldOverrideUrlLoading()。忽略除发送服务器URL之外的所有内容,此时您需要提取所需的数据(在我们的例子中是服务器验证的登录信息)。这在通话中可见GlobalState.getInstance().currentUserName = getCookieValue("_username" ,cookies);

大概不会很好地解释这一点(而且已经过去了一个月左右!)。这里是大部分的工作是做的SSOActivity的样本:

public class SSOActivity extends Activity { 
    WebView webView; 
    private Button mCancel; 
    private Button mReset; 

    /** 
    * Grabs the specified variables out of the list of cookies 
    * 
    * @param fieldName 
    * @param cookies 
    * @return 
    */ 
    public String getCookieValue(String fieldName, final String cookies){  
     String CookieValue = null; 

     String[] cookiessplit = cookies.split(";"); 
     for (String str : cookiessplit) { 
      if(str.contains(fieldName)) { 
       String[] value=str.split("="); 
       CookieValue = value[1]; 
       break; 
      } 
     }    
     return CookieValue; 
    } 

    public void clearCookies() { 
     try { 
      android.webkit.CookieManager cookieManager = CookieManager.getInstance(); 
      cookieManager.removeAllCookie(); 
     } 
     catch (Exception ex) 
     { 
      Utilities.logException(ex); 
      Utilities.logError("SSOActivity", "clearCookies() : " + ex.getMessage()); 
     } 
    } 

    /** 
    * Cancels the SSO request in Webview 
    * 
    * @param view 
    */ 
    public void cancelSSOClick (View view) { 
     Utilities.logInfo("cancelSSOClick", "Cancel SSO click"); 
     setResult(Activity.RESULT_CANCELED, null); 
     SSOActivity.this.finish(); 
    } 

    /** 
    * Resets and deletes cookies and SSOUrl if one exists 
    * 
    * @param view 
    */ 
    public void resetSSOClick (View view) { 
     Utilities.logInfo("resetSSOClick", "Cancel SSO click"); 
     setResult(Activity.RESULT_CANCELED, null); 
     clearCookies(); 
     SSOActivity.this.finish(); 
    } 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setResult(Activity.RESULT_OK, null); 

     // Setup the web view. It will redirect to SSO site for login 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_sso); 

     mCancel = (Button)findViewById(R.id.cancelSSO); 
     mCancel.setTextColor(Color.WHITE); 

     mReset = (Button)findViewById(R.id.resetSSO); 
     mReset.setTextColor(Color.WHITE); 

     webView = (WebView) findViewById(R.id.ssoViewer); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.getSettings().setSupportZoom(false); 
     webView.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading (WebView view, String url) { 
       try { 
        // If coming from our system, then we need to check the cookie for username password, for 
        // some SSO this might be different than the base url. Check for both 
        if (url.equals(Constants.getBaseUrl()) || url.equals(Constants.SSO_RETURN_URL)) { 

         CookieManager cookieManager = CookieManager.getInstance(); 
         final String cookies = cookieManager.getCookie(url); 
         GlobalState.getInstance().currentUserName = getCookieValue("_username" ,cookies); 
         SSOActivity.this.finish(); 
         return true; 
        } 
       } 
       catch (Exception ex) { 
        GlobalState.getInstance().currentUserName = ""; 
        GlobalState.getInstance().currentPassword = ""; 
        setResult(Activity.RESULT_CANCELED, null); 
        SSOActivity.this.finish(); 
       } 

       return false; 
      } 

     }); 

     try { 
      webView.loadUrl(Constants.SINGLE_SIGNON_URL); 
     } 
     catch (Exception ex) { 
      Utilities.logException(ex); 
      Utilities.logError("SSOActivity", "onCreate(), webView.loadUrl(ssoUrl) : " + ex.getMessage()); 

     } 

    } 

} 

这里是XML支持活动的一个例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/ssoViewerLayout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:id="@+id/button_layout" 
       android:layout_width="match_parent" 
       android:orientation="horizontal" 
       android:layout_height="wrap_content" 
       android:gravity="center|bottom" 
       android:layout_alignParentBottom="true">  

       <Button 
        android:id="@+id/cancelSSO" 
        android:layout_marginTop="16dp" 
        android:layout_width="125dp" 
        android:layout_height="55dp" 
        android:layout_margin="5dp" 
        android:onClick="cancelSSOClick" 
        android:text="Cancel Login" 
        android:background="@drawable/button_login" /> 
       <Button 
        android:id="@+id/resetSSO" 
        android:layout_marginTop="16dp" 
        android:layout_width="125dp" 
        android:layout_height="55dp" 
        android:layout_margin="5dp" 
        android:onClick="resetSSOClick" 
        android:text="Reset SSO" 
        android:background="@drawable/button_login"/> 
     </LinearLayout> 
    <WebView 
       android:id="@+id/ssoViewer" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_above="@id/button_layout" /> 
    </RelativeLayout> 

在代码看起来像这样别人调用它:

     Intent viewIntent = new Intent(getActivity(), SSOActivity.class); 
         (getActivity()).startActivityForResult(viewIntent, Constants.SINGLE_SIGN_ON); 

最后,你应该看到:

enter image description here

希望这会有所帮助!

+0

tx,今天就来看看会不会让你发贴 –