This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathOAuth2TokenRepository.swift
100 lines (90 loc) · 3.2 KB
/
OAuth2TokenRepository.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// OAuth2TokenRepository.swift
// reddift
//
// Created by sonson on 2015/04/14.
// Copyright (c) 2015年 sonson. All rights reserved.
//
import Foundation
import MiniKeychain
/**
Repository to contain OAuth2 tokens for reddit.com based on "KeychanAccess".
You can manage mulitple accounts using this class.
OAuth2TokenRepository, is utility class, has only class method.
*/
public class OAuth2TokenRepository {
/**
Restores token for OAuth2 from Keychain.
- parameter name: Specifies user name of token you want to restore from Keychain.
- returns: OAuth2Token object.
*/
public class func token(of name: String) throws -> OAuth2Token {
let keychain = Keychain(service: Config.sharedInstance.bundleIdentifier)
do {
let data = try keychain.data(of: name)
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? JSONDictionary {
return OAuth2Token(json)
}
throw ReddiftError.specifiedNameTokenNotFoundInKeychain as NSError
} catch {
throw error
}
}
/**
Restores user name list from Keychain.
- returns: List contains user names that was used to save tokens.
*/
public class var savedNames: [String] {
let keychain = Keychain(service: Config.sharedInstance.bundleIdentifier)
do {
return try keychain.keys()
} catch {
print(error)
return []
}
}
/**
Saves OAuth2 token object into Keychain.
- parameter token: OAuth2Token object, that must have valid user name which is used to save it into Keychain.
*/
public class func save(token: OAuth2Token) throws {
if token.name.isEmpty {
throw ReddiftError.tokenNameIsInvalid as NSError
}
do {
let data = try JSONSerialization.data(withJSONObject: token.JSONObject, options: [])
let keychain = Keychain(service: Config.sharedInstance.bundleIdentifier)
try keychain.save(key: token.name, data: data)
} catch {
throw error
}
}
/**
Saves OAuth2 token object into Keychain.
- parameter token: OAuth2Token object.
- parameter name: Valid user name which is used to save it into Keychain.
*/
public class func save(token: OAuth2Token, of name: String) throws {
if name.isEmpty {
throw ReddiftError.tokenNameIsInvalid as NSError
}
do {
let data = try JSONSerialization.data(withJSONObject: token.JSONObject, options: [])
let keychain = Keychain(service: Config.sharedInstance.bundleIdentifier)
try keychain.save(key: name, data: data)
} catch {
throw error
}
}
/**
Removes OAuth2 token whose user name is specified by the name parmeter from Keychain.
- parameter name: Valid user name which is used to save it into Keychain.
*/
public class func removeToken(of name: String) throws {
if name.isEmpty {
throw ReddiftError.tokenNameIsInvalid as NSError
}
let keychain = Keychain(service: Config.sharedInstance.bundleIdentifier)
keychain.delete(key: name)
}
}