https://github.com/fusionauth/fusionauth-swift-sdk
Swift SDK for iOS with FusionAuth
https://github.com/fusionauth/fusionauth-swift-sdk
authentication ios oauth oidc
Last synced: about 1 year ago
JSON representation
Swift SDK for iOS with FusionAuth
- Host: GitHub
- URL: https://github.com/fusionauth/fusionauth-swift-sdk
- Owner: FusionAuth
- License: other
- Created: 2024-04-03T09:04:19.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-14T13:18:47.000Z (about 1 year ago)
- Last Synced: 2025-04-20T16:09:27.818Z (about 1 year ago)
- Topics: authentication, ios, oauth, oidc
- Language: Swift
- Homepage: https://fusionauth.io
- Size: 425 KB
- Stars: 0
- Watchers: 10
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
[](https://github.com/FusionAuth/fusionauth-swift-sdk/tags)
[](https://github.com/FusionAuth/fusionauth-swift-sdk/network/updates)
[](https://github.com/FusionAuth/fusionauth-swift-sdk/pulls)
An SDK for using FusionAuth in iOS Apps.
# Table of Contents
- [Overview](#overview)
- [Getting Started](#getting-started)
- [Usage](#usage)
- [Example App](#example-app)
- [Quickstart](#quickstart)
- [Documentation](#documentation)
- [Contributing](#contributing)
- [Upgrade Policy](#upgrade-policy)
# Overview
This SDK allows you to use OAuth 2.0 and OpenId Connect functionality in an iOS app with FusionAuth as the
authorization server. It also provides a Token Manager to store, refresh, and retrieve tokens.
It's a highly standardized and simplified starting point for developers to easily integrate FusionAuth into their own custom mobile apps by taking care of all the dependencies.
Following OAuth 2.0 and OpenID Connect functionality are covered:
- OAuth 2.0 Authorization Code Grant
- OAuth 2.0 Refresh Token Grant
- OpenID Connect UserInfo
- OpenID Connect End Session
[AppAuth-iOS](https://github.com/openid/AppAuth-iOS) is used for the OAuth 2.0 Authorization Code Grant flow and OpenID Connect functionality.
The SDK is written in Swift and compatible with Object-C.
# Getting Started
To use the FusionAuth iOS SDK, add this repository as a dependency.
By default the SDK uses the `FusionAuth.plist` file in the main bundle to read the configuration. The file should contain the following keys:
```xml
additionalScopes
profile
email
fusionAuthUrl
http://localhost:9011
clientId
e9fdb985-9173-4e01-9d73-ac2d60d1dc8e
```
Then, initialize the AuthorizationManager in the `init` func of your app:
```swift
@main
struct QuickstartApp: App {
init() {
AuthorizationManager.instance.initialize()
}
}
```
By default, the SDK uses the `MemoryStorage for storing tokens. You can use one of the other available mechanisms `KeyChainStorage`or `UserDefaultsStorage` by e.g. adding the `storage` key with the value `keychain` or `userdefaults`.
As a alternative, you can also use the `initialize` function to provide the configuration:
```swift
@main
struct QuickstartApp: App {
init() {
AuthorizationManager.instance.initialize(configuration: AuthorizationConfiguration(
clientId: "e9fdb985-9173-4e01-9d73-ac2d60d1dc8e",
fusionAuthUrl: "http://localhost:9011",
additionalScopes: ["email", "profile"]))
}
}
```
Finally, instruct the app to handle the callback from the browser:
```swift
@main
struct QuickstartApp: App {
init() {
AuthorizationManager.instance.initialize()
}
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
OAuthAuthorization.resume(with: url)
}
}
}
}
```
# Usage
To start the OAuth 2.0 Authorization Code Grant, you can use the `oauth()` function on the `AuthorizationManager` to
retrieve the `OAuthAuthorizationService`:
```swift
do {
try await AuthorizationManager
.oauth()
.authorize(options: OAuthAuthorizeOptions())
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
```
The `authorize` method will open the Safari browser and redirect to the FusionAuth login page. After successful login, the user will be redirected back to the app with the authorization code. The SDK will exchange the authorization code for an access token and refresh token.
This will retrieve the authorization response, validates the `state` if it was provided, and exchanges the authorization
code for an access token.
The result of the exchange will be stored in the `TokenManager`.
After the user is authorized, you can use `getUserInfo()` to retrieve the [User Info](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo):
```swift
do {
self.userInfo = try await AuthorizationManager
.oauth()
.userInfo()
} catch let error as NSError {
print("JSON decode failed: \(error.localizedDescription)")
}
```
To call your API with an access token, you can use the `AuthorizationManager` to retrieve a valid access token:
```swift
let accessToken = AuthorizationManager.oauth().freshAccessToken()
```
This will retrieve a fresh access token from the `TokenManager` and return it. If the access token is expired,
the `TokenManager` will refresh it automatically.
Finally, you can use the `AuthorizationManager` to sign out the user and remove the tokens from the `TokenManager`:
```swift
do {
try await AuthorizationManager
.oauth()
.logout(options: OAuthLogoutOptions())
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
```
If the user is signed out, the auth state will be cleared.
# Example App
See the [FusionAuth iOS SDK Example](https://github.com/FusionAuth/fusionauth-quickstart-swift-ios-native) for a functional example of an iOS client that uses the SDK.
# Quickstart
See the [FusionAuth iOS Quickstart](https://fusionauth.io/docs/quickstarts/quickstart-swift-ios-native/) for a full tutorial on using FusionAuth and iOS.
# Documentation
See the latest [Full library documentation](https://github.com/FusionAuth/fusionauth-swift-sdk/blob/main/Documentation/Reference/README.md) for the complete documentation of the SDK.
# Contributing
We hope you love using FusionAuth Swift SDK, but in case you encounter a bug or an issue with the SDK, please do let us know.
Please follow the detailed [Contributing](CONTRIBUTING.md) documentation.
# Upgrade Policy
This library may periodically receive updates with bug fixes, security patches, tests, code samples, or documentation changes.
These releases may also update dependencies, language engines, and operating systems, as we\'ll follow the deprecation and sunsetting policies of the underlying technologies that the libraries use.
This means that after a dependency (e.g. language, framework, or operating system) is deprecated by its maintainer, this library will also be deprecated by us, and may eventually be updated to use a newer version.