-
Notifications
You must be signed in to change notification settings - Fork 1
/
BasicHTTPAuth.swift
57 lines (50 loc) 路 1.51 KB
/
BasicHTTPAuth.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// BasicHTTPAuth.swift
// Facestar
//
// Created by JohnP on 2/16/17.
// Copyright 漏 2017 JohnP. All rights reserved.
//
import Foundation
/**
A type that understands basic HTTP authentication: username and password.
*/
public protocol BasicHTTPAuthType {
var username: String { get }
var password: String { get }
}
public func == (lhs: BasicHTTPAuthType, rhs: BasicHTTPAuthType) -> Bool {
return type(of: lhs) == type(of: rhs) &&
lhs.username == rhs.username &&
lhs.password == rhs.password
}
public func == (lhs: BasicHTTPAuthType?, rhs: BasicHTTPAuthType?) -> Bool {
return type(of: lhs) == type(of: rhs) &&
lhs?.username == rhs?.username &&
lhs?.password == rhs?.password
}
extension BasicHTTPAuthType {
/**
Contents of the `Authorization` header needed to perform basic HTTP auth.
*/
var authorizationHeader: String? {
let string = "\(username):\(password)"
if let data = string.data(using: .utf8) {
let base64 = data.base64EncodedString(options: .lineLength64Characters)
return "Basic \(base64)"
}
return nil
}
}
public struct BasicHTTPAuth: BasicHTTPAuthType {
public let username: String
public let password: String
public static let development: BasicHTTPAuthType = BasicHTTPAuth(
username: "username",
password: "password"
)
public init(username: String, password: String) {
self.username = username
self.password = password
}
}