Skip to content

How to use NuguLoginKit for authorization

Yonghoon Kwon edited this page May 18, 2021 · 1 revision

Authorization Guide

Wrote based on v1.1.0 (updated 2021.05.18)

NuguLoginKit

NuguOAuthClient

To use NuguLoginKit, need NuguOAuthClient instance.
Use serviceName as the unique id of your service (prefer bundle identifier).
It is used to store and to load that created device-unique-ID in the iOS keychain.

let nuguOAuthClient = NuguOAuthClient(serviceName: "Bundle Identifier")

Usage depending on OAuth Grant-Type.

NuguLoginKit has supports three grant-type (authorization-code, refresh-token, client-credentials)

Authorization code

  • Grant Model
let authorizationCodeGrant = AuthorizationCodeGrant(clientId: "", clientSecret: "", redirectUri: "")
  • Preset for authorization
// AppDelegate.swift
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    NuguOAuthClient.handle(url: url)
    return true
}
  • Request authorization
nuguOAuthClient.authorize(grant: authorizationCodeGrant, parentViewController: vc) { (result) in
   switch result {
       case .success(let authInfo): // Succeed to login
       case .failure(let error): // Fail to login
   }
}

Refresh token

  • Grant Model
let refreshTokenGrant = RefreshTokenGrant(clientId: "", clientSecret: "", refreshToken: "")
  • Request authorization
oauthClient.authorize(grant: refreshTokenGrant) { (result) in
    switch result {
       case .success(let authInfo): // Succeed to login
       case .failure(let error): // Fail to login
   }
}

Client credentials

  • Grant Model
let clientCredentialsGrant = ClientCredentialsGrant(clientId: "", clientSecret: "")
  • Request authorization
oauthClient.authorize(grant: clientCredentialsGrant) { (result) in
    switch result {
       case .success(let authInfo): // Succeed to login
       case .failure(let error): // Fail to login
   }
}

Authorization-Info (Result of authorization)

If you are properly login, you can get the AuthorizationInfo.

public struct AuthorizationInfo {

    /// Token for networking with device-gateway.
    public let accessToken: String
    
    /// Type of access-token.
    public let tokenType: String
    
    /// Required to refresh the access-token.
    ///
    /// If has a `refreshToken`, can request oauth authentication that grant-type is refresh_token.
    public let refreshToken: String?
    
    /// The authorization server uses the "scope" response parameter to inform the client of the scope of the access token issued.
    public let scopes: [String]
    private let scope: String?

    /// Expiration date of access-token.
    public let expireDate: Date
}