Skip to content

Instantly share code, notes, and snippets.

View lapinek's full-sized avatar

Konstantin Lapine lapinek

View GitHub Profile
// ViewController.swift
// . . .
// MARK: Authorization methods
extension ViewController {
// . . .
func authorizeWithWebView(
// ViewController.swift
// . . .
// MARK: Conforming to WKScriptMessageHandler protocol
extension ViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print(#function)
if message.name == "callback", let messageBody = message.body as? String, let messageBodyData = messageBody.data(using: .utf8) {
// AuthenticationResponse.swift
import Foundation
/**
Container for JSON callbacks expected from the authentication endpoint.
*/
struct AuthenticationResponse: Codable {
struct Callback: Codable {
let type: String?
// WebViewController.js
(function () {
if (window.webkit) {
var response = {
"authId": "authId",
"callbacks": [
{
"type": "Code",
"input": [],
// ViewController.swift
// . . .
// MARK: Authorization methods
extension ViewController {
// . . .
/**
Authorizes the Relying Party with an OIDC Provider.
// ViewController.swift
// . . .
// MARK: Delegate for the web view navigation events.
extension ViewController: WKNavigationDelegate {
// . . .
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print("Navigation—action URL: ", navigationAction.request.url?.absoluteString ?? "")
// ViewController.swift
// . . .
import WebKit
// . . .
// ViewController.swift
// . . .
// MARK: Authorization methods
extension ViewController {
// . . .
/**
Performs the authorization code flow using a web view.
// ViewController.swift
// . . .
class ViewController: UIViewController {
// . . .
/**
App Group name.
// ViewController.swift
import UIKit
class ViewController: UIViewController {
var webViewController: WebViewController!
// . . .
/**