This repository is now archived as my time has become limited and my interest in Apple development has waned.
SwAuth is an OAuth 2.0 HTTP request library written in Swift for iOS 13.0+, macOS 10.15+, watchOS 6.0+, and tvOS 13.0+.
SwAuth has build in support for some Web API's with more to come:
Use the Swift Package Manager to add SwAuth to your project! Simply add the package to dependencies in your Package.swift
:
// swift-tools-version:5.5
import PackageDescription
let package = Package(
name: "YourPackageName",
products: [
.library(name: "YourPackageName", targets: ["YourPackageName"]),
],
dependencies: [
.package(url: "https://github.com/Colaski/SwAuth.git", from: "1.0.1"),
],
targets: [
.target(name: "YourPackageName", dependencies: ["SwAuth"]),
]
)
Select File > Add Packages
in Xcode and enter https://github.com/Colaski/SwAuth.git
Xcode should take care of the rest!
Import SwAuth in files you wish to use it's amazing features:
import SwAuth
Create an instance of keychain:
let keychain = Keychain(service: "com.your.bundleID",
accessGroup: "appIdentifierPrefix.com.your.bundleID").label("Your App Name")
SwAuth uses KeychainAccess to make setting up the keychain easy. You can see a full list of options for it here: https://github.com/kishikawakatsumi/KeychainAccess#key-configuration-accessibility-sharing-icloud-sync.
Create an instance of the proper authorization flow for your Web API.
let keychain = Keychain(service: "com.your.bundleID",
accessGroup: "appIdentifierPrefix.com.your.bundleID").label("Your App Name")
var spotify = PKCEAuthorizationFlow(clientID: "YourClientID",
authorizationEndpoint: URL(string: "https://accounts.spotify.com/authorize")!,
tokenEndpoint: URL(string: "https://accounts.spotify.com/api/token")!,
redirectURI: "someapp://callback",
keychain: keychain,
scopes: "user-follow-modify")
spotify.additionalRefreshTokenBodyParams = ["client_id": "YourClientID"] // Spotify specifically requires the client ID to be included in the refresh token's body parameters.
Start an ASWebAuthenticationSession like in the example app with the instance's authorization URL:
spotify.authorizationURL
Pass the callback URL from the ASWebAuthenticationSession into the provided handler method:
do {
try await spotify.authorizationResponseHandler(for: callbackURL)
} catch {
print(error.localizedDescription)
}
Make an authorized request:
do {
// https://developer.spotify.com/documentation/web-api/reference/#/operations/follow-artists-users
var request = HTTPRequest(endpoint: URL(sting: "https://api.spotify.com/v1/me/following")!)
request.httpMethod = .PUT
request.endpointQueryItems = ["type": "artist"]
request.httpBody = ["ids": ["5K4W6rqBFWDnAN6FQUkS6x"]]
request.bodyEncoding = .JSON
// Send an authenticated HTTP request, this one will follow the artist Kanye West on Spotify.
let json = try await spotify.authenticatedRequest(for: request, numberOfRetries: 2).json()
// Prints the JSON output
print(json)
} catch {
print(error.localizedDescription)
}
For more information, read my beautiful documentation: https://swauth.netlify.app/documentation/Swauth
Check out CONTRIBUTING.md for information!
SwAuth its self is licensed under the MIT License, however please take notice of the NOTICE file in the root of this repository. Also, make sure to check the respective licenses of this library's dependencies before releasing your project.
link |
Stars: 16 |
Last commit: 1 year ago |
Xcode 13.2 update
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics