Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Fixed registration endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcook committed Feb 12, 2017
1 parent ded99f2 commit a3ed535
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
16 changes: 8 additions & 8 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
PODS:
- BeamAPI (1.4.9):
- BeamAPI (1.4.10):
- Starscream (~> 2.0)
- SwiftyJSON (~> 3.1)
- Starscream (2.0.2)
- SwiftyJSON (3.1.3)
- Starscream (2.0.3)
- SwiftyJSON (3.1.4)

DEPENDENCIES:
- BeamAPI (from `../`)

EXTERNAL SOURCES:
BeamAPI:
:path: ../
:path: "../"

SPEC CHECKSUMS:
BeamAPI: 19153ab4ea5074ac448ef9659bff69a93e98bf8f
Starscream: 6c135a34e0a6e60cedaa0b30db67a4c05cf7cd38
SwiftyJSON: 38a8ea2006779c0fc4c310cb2ee8195327740faf
BeamAPI: 33841b5f49d372fa51e99a9b44d634d96eaa81f3
Starscream: 3fdd5c277e57cca6b5c406d274e3f34a7c88f2ce
SwiftyJSON: c2842d878f95482ffceec5709abc3d05680c0220

PODFILE CHECKSUM: a1a8f717d98a3dc71349b5bc00cee46e3733b0df

COCOAPODS: 1.1.1
COCOAPODS: 1.2.0
13 changes: 1 addition & 12 deletions Example/Tests/SessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import XCTest
class SessionTests: XCTestCase {

var username: String!
let invalidUsername = "a"
let invalidUsername = "xcz"
let takenUsername = "beamtest"

var password: String!
Expand Down Expand Up @@ -79,17 +79,6 @@ class SessionTests: XCTestCase {
waitForExpectations(timeout: 10, handler: nil)
}

func testRegisterInvalidUsername() {
let expectation = self.expectation(description: "tests the registration endpoint for the invalid username error")

BeamSession.registerAccount(invalidUsername, password: password, email: email) { (user, error) in
XCTAssert(error == .invalidUsername)
expectation.fulfill()
}

waitForExpectations(timeout: 10, handler: nil)
}

func testRegisterTakenUsername() {
let expectation = self.expectation(description: "tests the registration endpoint for the taken username error")

Expand Down
2 changes: 1 addition & 1 deletion Pod/Classes/Clients/BeamSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public class BeamSession {
"email": email
]

BeamRequest.request("/users", requestType: "POST", body: body as AnyObject) { (json, error) in
BeamRequest.request("/users", requestType: "POST", body: body as AnyObject, options: .mayNeedCSRF) { (json, error) in
guard error == nil,
let json = json else {
completion?(nil, error)
Expand Down
18 changes: 15 additions & 3 deletions Pod/Classes/Utilities/BeamRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class BeamRequest {
:param: options Any special operations that should be performed for this request.
:param: completion An optional completion block with retrieved data.
*/
public class func dataRequest(_ baseURL: String, requestType: String = "GET", headers: [String: String] = [String: String](), params: [String: String] = [String: String](), body: AnyObject? = nil, options: BeamRequestOptions = [], completion: ((_ data: Data?, _ error: BeamRequestError?) -> Void)?) {
public class func dataRequest(_ baseURL: String, requestType: String = "GET", headers: [String: String] = [String: String](), params: [String: String] = [String: String](), body: AnyObject? = nil, options: BeamRequestOptions = [], csrfToken: String? = nil, completion: ((_ data: Data?, _ error: BeamRequestError?) -> Void)?) {
let sessionConfig = URLSessionConfiguration.ephemeral
let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)

Expand Down Expand Up @@ -140,6 +140,10 @@ public class BeamRequest {
}
}

if let token = csrfToken {
request.addValue(token, forHTTPHeaderField: "x-csrf-token")
}

let task = session.dataTask(with: request) { (data, response, error) in
guard let response = response as? HTTPURLResponse, let data = data else {
completion?(nil, .unknown(data: nil))
Expand All @@ -153,7 +157,7 @@ public class BeamRequest {
return
}

if options.contains(.storeCookies) {
if options.contains(.storeCookies) || options.contains(.mayNeedCSRF) {
let cookies = HTTPCookie.cookies(withResponseHeaderFields: response.allHeaderFields as! [String : String], for: url)
var storedCookies = [[HTTPCookiePropertyKey: Any]]()

Expand All @@ -175,6 +179,8 @@ public class BeamRequest {
let json = JSON(data: data)
var requestError: BeamRequestError = .unknown(data: json)

print("\(csrfToken) vs. \(response.allHeaderFields["x-csrf-token"])")

if let error = error {
switch error._code {
case -1009: requestError = .offline
Expand All @@ -200,7 +206,6 @@ public class BeamRequest {
}
case "payload.username":
switch type {
case "string.min": requestError = .invalidUsername
case "unique": requestError = .takenUsername
default: requestError = .unknown(data: json)
}
Expand Down Expand Up @@ -245,11 +250,18 @@ public class BeamRequest {
dataRequest(baseURL, requestType: requestType, headers: headers, params: params, body: body, options: options, completion: completion)
}
}

return
} else {
requestError = .invalidCredentials
}
case 403: requestError = .accessDenied
case 404: requestError = .notFound
case 461:
if options.contains(.mayNeedCSRF), let token = response.allHeaderFields["x-csrf-token"] as? String {
dataRequest(baseURL, requestType: requestType, headers: headers, params: params, body: body, options: [.cookieAuth, .storeCookies], csrfToken: token, completion: completion)
return
}
case 499: requestError = .requires2FA
default:
print("Unknown status code: \(response.statusCode)")
Expand Down
4 changes: 1 addition & 3 deletions Pod/Classes/Utilities/BeamRequestError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public enum BeamRequestError: Equatable, Error {
requires2FA

// Registration Errors
case invalidUsername,
reservedUsername,
case reservedUsername,
takenUsername,
weakPassword,
invalidEmail,
Expand All @@ -45,7 +44,6 @@ public func ==(lhs: BeamRequestError, rhs: BeamRequestError) -> Bool {
case (.unknown(_), .unknown(_)): return true
case (.invalidCredentials, .invalidCredentials): return true
case (.requires2FA, .requires2FA): return true
case (.invalidUsername, .invalidUsername): return true
case (.reservedUsername, .reservedUsername): return true
case (.takenUsername, .takenUsername): return true
case (.weakPassword, .weakPassword): return true
Expand Down
3 changes: 3 additions & 0 deletions Pod/Classes/Utilities/BeamRequestOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ public struct BeamRequestOptions: OptionSet {

// Store any received JWT tokens
public static let storeJWT = BeamRequestOptions(rawValue: 1 << 3)

// Request may need a CSRF token
public static let mayNeedCSRF = BeamRequestOptions(rawValue: 1 << 4)
}

0 comments on commit a3ed535

Please sign in to comment.