Created
June 28, 2018 08:54
-
-
Save jonnyijapan/8330876807be71a8237b26b5f13d70ad to your computer and use it in GitHub Desktop.
Excavate twitterkit oauth session details
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MigrateTwitterkitSession.swift | |
// | |
// | |
// Created by jonnyijapan on 2018/06/28. | |
// Copyright © 2018 jonnyijapan. All rights reserved. | |
// | |
import Foundation | |
import KeychainAccess // https://github.com/kishikawakatsumi/KeychainAccess | |
class MigratedTwitterkitSession: NSObject, NSCoding { | |
let authToken: String | |
let authTokenSecret: String | |
let userName: String | |
enum Result { | |
case migrated(session: MigratedTwitterkitSession) | |
case nosession(errors: [Error]) | |
} | |
/// Searches for any existing TwitterKit OAuth session. If found, returns it after deleting all of the TwitterKit service data. | |
/// | |
/// - Returns: A Result object. If result is migrated, the session parameter containts a TWTRSession object consisting of OAuth key and secret, and Twitter screenname. | |
static func tryToMigrateSession() -> Result { | |
NSKeyedUnarchiver.setClass(MigratedTwitterkitSession.self, forClassName: "TWTRSession") | |
let keychain = Keychain.init(service: "com.twitter.api-service.user-session-store-service-name") | |
//print("keychain.allKeys(): \(keychain.allKeys())") | |
var errors = [Error]() | |
for key in keychain.allKeys() { | |
if let value: Data = keychain[data: key] { | |
//print("key: \(key), data value: \(value)") | |
let unarchiver = NSKeyedUnarchiver.init(forReadingWith: value) | |
do { | |
let obj = try unarchiver.decodeTopLevelObject(forKey: "root") | |
if let object = obj { | |
//print("Unarchived root object: \(object)") | |
if let session = object as? MigratedTwitterkitSession { | |
//print("Got session session.authToken: \(session.authToken), session.userName: \(session.userName), session.authTokenSecret: \(session.authTokenSecret)") | |
try keychain.removeAll() | |
return .migrated(session: session) | |
} | |
} | |
else { | |
print("Failed unarchiving root object") | |
} | |
} | |
catch { | |
errors.append(error) | |
} | |
} | |
} | |
return .nosession(errors: errors) | |
} | |
func encode(with aCoder: NSCoder) { | |
} | |
required init?(coder aDecoder: NSCoder) { | |
if let token = aDecoder.decodeObject(forKey: "oauth_token") as? String, let secret = aDecoder.decodeObject(forKey: "oauth_token_secret") as? String, let screenname = aDecoder.decodeObject(forKey: "screen_name") as? String { | |
self.authToken = token | |
self.authTokenSecret = secret | |
self.userName = screenname | |
} | |
else { | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
my account has been suspended
I think the reason is a lot of Twitter, Twitter and follow-up
I would like to apologize to you
If you would give me another chance
Thank you