Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F13819832
WriteFreelyModel+Keychain.swift
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
WriteFreelyModel+Keychain.swift
View Options
import
Foundation
extension
WriteFreelyModel
{
func
saveTokenToKeychain
(
_
token
:
String
,
username
:
String
?,
server
:
String
)
throws
{
let
query
:
[
String
:
Any
]
=
[
kSecClass
as
String
:
kSecClassGenericPassword
,
kSecValueData
as
String
:
token
.
data
(
using
:
.
utf8
)
!
,
kSecAttrAccount
as
String
:
username
??
"anonymous"
,
kSecAttrService
as
String
:
server
]
let
status
=
SecItemAdd
(
query
as
CFDictionary
,
nil
)
guard
status
==
errSecDuplicateItem
||
status
==
errSecSuccess
else
{
throw
KeychainError
.
couldNotStoreAccessToken
}
}
func
purgeTokenFromKeychain
(
username
:
String
?,
server
:
String
)
throws
{
let
query
:
[
String
:
Any
]
=
[
kSecClass
as
String
:
kSecClassGenericPassword
,
kSecAttrAccount
as
String
:
username
??
"anonymous"
,
kSecAttrService
as
String
:
server
]
let
status
=
SecItemDelete
(
query
as
CFDictionary
)
guard
status
==
errSecSuccess
||
status
==
errSecItemNotFound
else
{
throw
KeychainError
.
couldNotPurgeAccessToken
}
}
func
fetchTokenFromKeychain
(
username
:
String
?,
server
:
String
)
throws
->
String
?
{
let
query
:
[
String
:
Any
]
=
[
kSecClass
as
String
:
kSecClassGenericPassword
,
kSecAttrAccount
as
String
:
username
??
"anonymous"
,
kSecAttrService
as
String
:
server
,
kSecMatchLimit
as
String
:
kSecMatchLimitOne
,
kSecReturnAttributes
as
String
:
true
,
kSecReturnData
as
String
:
true
]
var
secItem
:
CFTypeRef
?
let
status
=
SecItemCopyMatching
(
query
as
CFDictionary
,
&
secItem
)
guard
status
!=
errSecItemNotFound
else
{
throw
KeychainError
.
couldNotFetchAccessToken
}
guard
status
==
errSecSuccess
else
{
throw
KeychainError
.
couldNotFetchAccessToken
}
guard
let
existingSecItem
=
secItem
as
?
[
String
:
Any
],
let
tokenData
=
existingSecItem
[
kSecValueData
as
String
]
as
?
Data
,
let
token
=
String
(
data
:
tokenData
,
encoding
:
.
utf8
)
else
{
throw
KeychainError
.
couldNotFetchAccessToken
}
return
token
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Feb 16, 5:38 AM (1 d, 19 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3629273
Attached To
rWFSUI WriteFreely SwiftUI
Event Timeline
Log In to Comment