Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. import Foundation
  2. import AWSCognitoIdentityProvider
  3. import AWSCognito
  4.  
  5. class AWSCustomIdentityProvider: NSObject, AWSIdentityProviderManager
  6. {
  7.  
  8. private var dict = NSDictionary()
  9.  
  10. func addToken(value:NSString)
  11. {
  12. dict = ["graph.facebook.com":value]
  13. }
  14.  
  15. public func logins() -> AWSTask<NSDictionary>
  16. {
  17. return AWSTask(result: dict)
  18. }
  19. }
  20.  
  21. public func loginButtonDidCompleteLogin(_ loginButton: FacebookLogin.LoginButton, result: FacebookLogin.LoginResult){
  22.  
  23. switch result {
  24. case .failed(let error):
  25. print("FACEBOOK LOGIN FAILED: (error)")
  26. case .cancelled:
  27. print("User cancelled login.")
  28. case .success(_, _, let accessToken):
  29.  
  30.  
  31. let customIdentity = AWSCustomIdentityProvider()
  32. let token = accessToken.authenticationToken
  33. customIdentity.addToken(value: token as NSString)
  34.  
  35. let credentialsProvider = AWSCognitoCredentialsProvider(regionType: REGIONTYPE, identityPoolId: "XXXXXXXXXXXXXXXXXXXXXXX", identityProviderManager:customIdentity)
  36.  
  37. credentialsProvider.clearKeychain()
  38. credentialsProvider.clearCredentials()
  39.  
  40. let serviceConfiguration = AWSServiceConfiguration(region: REDIONTYPE, credentialsProvider: credentialsProvider)
  41. AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration;
  42.  
  43. credentialsProvider.getIdentityId().continue( { (task: AWSTask!) -> AnyObject! in
  44. if (task.error != nil) {
  45. print("Error: " + (task.error?.localizedDescription)!)// gets called
  46. }
  47. else {
  48. print(task.result)//identityid
  49. }
  50. return nil
  51. })
  52. }
  53. }
  54.  
  55. import AWSLambda
  56. import Foundation
  57. struct AWSHelper{
  58. let lambda = AWSLambda.default()
  59. let APPLICATION_NAME = "MYAPPLICATION"
  60. init(){
  61.  
  62. }
  63.  
  64. func getFunctionName(funcName: String) -> String{
  65. return "(funcName)_(APPLICATION_NAME)"
  66. }
  67.  
  68. func login(facebookID: String, cognitoID:String, callback:@escaping (Bool) -> Void){
  69. let req = AWSLambdaInvocationRequest();
  70. req?.invocationType = AWSLambdaInvocationType.requestResponse
  71. req?.payload = ["cognitoID" : cognitoID, "facebookID" : facebookID]
  72. req?.functionName = getFunctionName(funcName: "MYFUNCNAME")
  73.  
  74. lambda.invoke(req!) { (response: AWSLambdaInvocationResponse?,error: Error?) in
  75. print(error)
  76. let payload = response?.payload
  77. print(payload)
  78. callback(true)
  79. }
  80.  
  81. }
  82. }
  83.  
  84. credentialsProvider.credentials().continue({ (task: AWSTask!) -> Any? in
  85. print(task.result)
  86. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement