/Rocket.Chat/Models/Mapping/LoginServiceModelMapping.swift

https://github.com/RocketChat/Rocket.Chat.iOS · Swift · 164 lines · 124 code · 29 blank · 11 comment · 11 complexity · 114fb85b2516468efb4fc21577df085e MD5 · raw file

  1. //
  2. // LoginServiceModelMapping.swift
  3. // Rocket.Chat
  4. //
  5. // Created by Matheus Cardoso on 10/17/17.
  6. // Copyright © 2017 Rocket.Chat. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftyJSON
  10. import RealmSwift
  11. extension LoginService: ModelMappeable {
  12. // swiftlint:disable cyclomatic_complexity
  13. func map(_ values: JSON, realm: Realm?) {
  14. if identifier == nil {
  15. identifier = values["_id"].string ?? values["id"].string
  16. }
  17. service = values["name"].string ?? values["service"].string
  18. clientId = values["appId"].string ?? values["clientId"].string
  19. custom = values["custom"].boolValue
  20. serverUrl = values["serverURL"].string
  21. tokenPath = values["tokenPath"].string
  22. authorizePath = values["authorizePath"].string
  23. scope = values["scope"].string
  24. buttonLabelText = values["buttonLabelText"].stringValue
  25. buttonLabelColor = values["buttonLabelColor"].stringValue
  26. tokenSentVia = values["tokenSentVia"].stringValue
  27. usernameField = values["usernameField"].stringValue
  28. mergeUsers = values["mergeUsers"].boolValue
  29. loginStyle = values["loginStyle"].string
  30. buttonColor = values["buttonColor"].string
  31. // CAS
  32. loginUrl = values["login_url"].string
  33. // SAML
  34. entryPoint = values["entryPoint"].string
  35. issuer = values["issuer"].string
  36. provider = values["clientConfig"]["provider"].string
  37. switch type {
  38. case .google: mapGoogle()
  39. case .facebook: mapFacebook()
  40. case .gitlab: mapGitLab()
  41. case .github: mapGitHub()
  42. case .linkedin: mapLinkedIn()
  43. case .wordpress:
  44. break // not mapped here since it needs a public setting for type
  45. case .saml: break
  46. case .cas: break
  47. case .custom: break
  48. case .invalid: break
  49. }
  50. }
  51. // swiftlint:enable cyclomatic_complexity
  52. func mapGoogle() {
  53. service = "google"
  54. scope = "email profile"
  55. serverUrl = "https://accounts.google.com"
  56. tokenPath = "/login/oauth/access_token"
  57. authorizePath = "/o/oauth2/v2/auth"
  58. buttonLabelText = "google"
  59. buttonLabelColor = "#ffffff"
  60. buttonColor = "#dd4b39"
  61. callbackPath = "google?close"
  62. }
  63. func mapGitHub() {
  64. service = "github"
  65. scope = ""
  66. serverUrl = "https://github.com"
  67. tokenPath = "/login/oauth/access_token"
  68. authorizePath = "/login/oauth/authorize"
  69. buttonLabelText = "github"
  70. buttonLabelColor = "#ffffff"
  71. buttonColor = "#4c4c4c"
  72. }
  73. func mapGitLab() {
  74. service = "gitlab"
  75. scope = "read_user"
  76. serverUrl = "https://gitlab.com"
  77. tokenPath = "/oauth/token"
  78. authorizePath = "/oauth/authorize"
  79. buttonLabelText = "gitlab"
  80. buttonLabelColor = "#ffffff"
  81. buttonColor = "#373d47"
  82. callbackPath = "gitlab?close"
  83. }
  84. func mapFacebook() {
  85. service = "facebook"
  86. scope = ""
  87. serverUrl = "https://facebook.com"
  88. scope = "email"
  89. tokenPath = "https://graph.facebook.com/oauth/v2/accessToken"
  90. authorizePath = "/v2.9/dialog/oauth"
  91. buttonLabelText = "facebook"
  92. buttonLabelColor = "#ffffff"
  93. buttonColor = "#325c99"
  94. callbackPath = "facebook?close"
  95. }
  96. func mapLinkedIn() {
  97. service = "linkedin"
  98. scope = ""
  99. serverUrl = "https://linkedin.com"
  100. tokenPath = "/oauth/v2/accessToken"
  101. authorizePath = "/oauth/v2/authorization"
  102. buttonLabelText = "linkedin"
  103. buttonLabelColor = "#ffffff"
  104. buttonColor = "#1b86bc"
  105. callbackPath = "linkedin?close"
  106. }
  107. func mapWordPress() {
  108. service = "wordpress"
  109. scope = scope ?? "auth"
  110. serverUrl = "https://public-api.wordpress.com"
  111. tokenPath = "/oauth2/token"
  112. authorizePath = "/oauth2/authorize"
  113. buttonLabelText = "wordpress"
  114. buttonLabelColor = "#ffffff"
  115. buttonColor = "#1e8cbe"
  116. callbackPath = "wordpress?close"
  117. }
  118. func mapWordPressCustom() {
  119. service = "wordpress"
  120. scope = scope ?? "openid"
  121. serverUrl = serverUrl ?? "https://public-api.wordpress.com"
  122. tokenPath = tokenPath ?? "/oauth/token"
  123. authorizePath = authorizePath ?? "/oauth/authorize"
  124. buttonLabelText = "wordpress"
  125. buttonLabelColor = "#ffffff"
  126. buttonColor = "#1e8cbe"
  127. callbackPath = "wordpress?close"
  128. }
  129. func mapCAS() {
  130. service = "cas"
  131. buttonLabelText = "CAS"
  132. buttonLabelColor = "#ffffff"
  133. buttonColor = "#13679a"
  134. }
  135. }