OAuthClient.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. import Foundation
  34. func escape(_ string : String) -> String {
  35. let dotsAndDashes = CharacterSet(charactersIn: "-._")
  36. let allowedCharacters = dotsAndDashes.union(.alphanumerics)
  37. if let escapedString = string.addingPercentEncoding(withAllowedCharacters: allowedCharacters) {
  38. return escapedString
  39. } else {
  40. return string
  41. }
  42. }
  43. class OAuthClient {
  44. private var clientID : String = ""
  45. private var clientSecret : String = ""
  46. private var redirectURIs : [String] = []
  47. private var authURI : String = ""
  48. private var tokenURI : String = ""
  49. public var token : String? = nil
  50. init () {
  51. clientID = "885917370891-n3r74v6miibn2969estdofr68ggqa1sn.apps.googleusercontent.com"
  52. clientSecret = "_JDxU8iGdHYfeeER9AAEaHbn"
  53. redirectURIs = ["http://localhost"]
  54. authURI = "https://accounts.google.com/o/oauth2/auth"
  55. tokenURI = "https://accounts.google.com/o/oauth2/token"
  56. }
  57. let scope = "https://www.googleapis.com/auth/datastore"
  58. func authCodeURL(state : String) -> URL? {
  59. var path = authURI
  60. path = path + "?response_type=" + "code"
  61. path = path + "&client_id=" + clientID
  62. path = path + "&redirect_uri=" + escape(redirectURIs[0])
  63. path = path + "&scope=" + escape(scope)
  64. path = path + "&state=" + state
  65. return URL(string:path)
  66. }
  67. func exchangeCode(code: String) {
  68. let path = tokenURI
  69. var body = "client_id=" + clientID
  70. body = body + "&client_secret=" + clientSecret
  71. body = body + "&code=" + escape(code)
  72. body = body + "&grant_type=" + "authorization_code"
  73. body = body + "&redirect_uri=" + escape(redirectURIs[0])
  74. let url = URL(string:path)!
  75. var request = URLRequest(url:url)
  76. request.httpMethod = "POST"
  77. request.httpBody = body.data(using:.utf8)
  78. let task = URLSession.shared.dataTask(with:request) { (data, response, error) in
  79. var json: [String:Any]!
  80. do {
  81. json = try JSONSerialization.jsonObject(with:data!, options: JSONSerialization.ReadingOptions()) as? Dictionary
  82. } catch {
  83. print(error)
  84. }
  85. self.token = json["access_token"] as! String?
  86. }
  87. task.resume()
  88. }
  89. }