This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Single Sign-On for Native iOS Applications with WSO2 Identity Server
Single sign-on (SSO) is an authentication process that allows a user to access multiple applications using the same set of credentials. With SSO, a user can provide credentials once and gain access to different applications without having to sign in to each application individually.
You can add SSO capability to your iOS based native client applications using WSO2 Identity Server as an identity provider.
This tutorial demonstrates how you can add SSO support to a sample iOS application using WSO2 Identity Server as the Identity Provider.
The tutorial includes the following sections:
Prerequisites
Before you start the tutorial, be sure to complete the following prerequisites:
Set up a MacOS based computer to try out the tutorial.
Download and install WSO2 Identity Server, which will act as the identity provider in the tutorial. You can download the product installer from here, and run it.
Download or clone the WSO2 Identity Server samples repository.
Download and install Xcode 9+ to work with the iOS client app sample.
It is useful to have basic knowledge in iOS application development and some experience with the SWIFT (3+) programming language.
Once you have the prerequisites, you can follow the step-by-step instructions in the following sections.
Setting up WSO2 Identity Server
Since WSO2 Identity Server acts as the identity provider in this tutorial, first you need to configure WSO2 Identity Server as an identity provider.
Let’s refer to the WSO2 Identity Server installation location as <IS_HOME> throughout the tutorial.
Before you begin, you need to set up a valid SSL certificate in WSO2 Identity Server. This is important because by default iOS applications are restricted from communicating with a source that does not have a valid certificate. However, if you are running the server locally and you want to test the sample application via the iOS simulator, you can add a self-signed certificate to both WSO2 Identity Server and the simulator.
Follow the steps here to add a self-signed certificate to WSO2 Identity Server.
Start WSO2 Identity Server and access the management console. You can sign in using
adminas the username and password. For detailed instructions on accessing the management console of WSO2 Identity Server, see Accessing the Management Console.On the Main tab of the management console, go to Identity -> Service Provider and click Add.
Enter an appropriate name for the new service provider and click Register.
Expand the Inbound Authentication Configuration section, and then expand the OAuth2/OpenID Connect Configuration.
Click Configure and set the following values in addition to the values that are set by default:
Specify
wso2issample://oauthas the Callback Url. This is the sample application’s URL.Select PKCE Mandatory. This is recommended because you need to adhere to the PKCE protocol as a best practice when developing native mobile applications.
Select Allow authentication without the client secret. This allows the native mobile application to bypass the authentication phase. For information on how native applications should interact with an authorization endpoint, see OAuth 2.0 for native apps specification.
Click Add. You will see an information message that says Application registered successfully.
Click OK.
Make a note of the generated OAuth Client Key. You need this value when you set up the sample application.
Now you have set up WSO2 Identity Server. Next you need to set up the sample iOS application.
Setting up the sample application
Use this tool to import a self-signed certificate to the iOS simulator.
Go to the downloaded WSO2 Identity Server samples repository. Let’s refer to the samples repository directory location as
<SAMPLES_HOME>throughout this tutorial.Open the
<SAMPLES_HOME>/oidc-client-app-samples/ios-client-app-sample/WSO2-IS-SampleApp.xcworkspacefile in Xcode.
Following is the directory structure of the sample application project:
Following are descriptions of the key components of the sample application project:
AppDelegate.swift- The default delegation class to observe changes of the sample application state.Main.storyboard- The main storyboard of the sample application.Info.plist- Contains all the essential details about the sample application.Config.plist- The configuration file that contains identity provider details along with the OAuth client ID.controllersdirectory - Contains controllers for the login view and profile view.modeldirectory - Contains model data objects.mgtdirectory - Contains management classes.utilsdirectory - Contains utility classes.resourcesdirectory - Contains the resource files used in the application.servicedirectory - Contains the service class that handles the OAuth communication. We will take a look at the purpose of this directory in detail later in the tutorial.
Now that you are familiar with the key components of the sample project, let's set up the dependencies and third party libraries that are required.
The sample client application uses CocoaPods as the dependency manager in the project and the dependencies are defined in the pods file. In this tutorial you do not need to install the dependencies because the dependencies are already included along with the sample application source code.
The sample client application project uses AppAuth as the third party library for OAuth 2.0 communication between the client and server. The AppAuth library is added as a dependency in the pod file.
Configuring the sample application
There are two key configuration steps that you need to perform before you run the application.
Configuring application settings
To configure application settings, right click on the <SAMPLES_HOME>/oidc-client-app-samples/ios-client-app-sample/WSO2-IS-SampleApp/Info.plist file and open it with a source code editor.
In the source code, you will see the following lines of code:
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string>wso2is.local</string> <key>CFBundleURLSchemes</key> <array> <string>wso2issample</string> </array> </dict> </array>Here, the
CFBundleURLTypesproperty specifies the URL scheme of the application. The URL scheme defines the manner in which applications communicate with each other. The sample application uses a custom URL scheme calledwso2issample.This is why you had to specify
wso2issample://oauthas the callback URL when you created the OAuth service provider in the Setting up WSO2 Identity Server section. When authentication is done, users are redirected to the sample application through thewso2issample://oauthURL.You will also see the following lines of code:
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>The above code block specifies that you allow the application to connect to sources with untrusted certificates. You can only use the above code block in a test environment.
Configuring endpoints and OAuth client settings
To configure endpoint and OAuth client settings, right click on the <SAMPLES_HOME>/oidc-client-app-samples/ios-client-app-sample/WSO2-IS-SampleApp/Config.plist file and open it with a source code editor.
In the source code, you will see several OAuth related properties defined as follows:
<dict> <key>IssuerURL</key> <string>https://localhost:9443</string> <key>LogOutURL</key> <string>https://localhost:9443/oidc/logout</string> <key>UserInfoURL</key> <string>https://localhost:9443/oauth2/userinfo?schema=openid</string> <key>TokenURL</key> <string>https://localhost:9443/oauth2/token</string> <key>AuthURL</key> <string>https://localhost:9443/oauth2/authorize</string> <key>RedirectURL</key> <string>wso2issample://oauth</string> <key>ClientID</key> <string>[YOUR_OAUTH_CLIENT_ID]</string> </dict>The first few properties are endpoints of the WSO2 Identity Server OAuth API. You can replace these values depending on your WSO2 Identity Server setup.
The
RedirectURLshould be the same URL that you specified as the callback URL when you created the OAuth service provider in the Setting up WSO2 Identity Server section.The
ClientIDshould be the OAuth client ID that you obtained in the Setting up WSO2 Identity Server section.