forked from OAuthSwift/OAuthSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestServer.swift
107 lines (84 loc) · 3.37 KB
/
TestServer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// TestServer.swift
// OAuthSwift
//
// Created by phimage on 17/11/15.
// Copyright © 2015 Dongri Jin. All rights reserved.
//
import Foundation
import Swifter
class TestServer {
let server: HttpServer
var port: in_port_t = 8766
var baseurl: String { return "http://localhost:\(self.port)/" }
var v1: String { return "\(baseurl)1/" }
var authorizeURL: String { return "\(v1)authorize" }
var accessTokenURL: String { return "\(v1)accessToken" }
var requestTokenURL: String { return "\(v1)requestToken" }
var v2: String { return "\(baseurl)2/" }
var authorizeURLV2: String { return "\(v2)authorize" }
var accessTokenURLV2: String { return "\(v2)accessToken" }
var expireURLV2: String { return "\(v2)expire" }
enum AccessReturnType {
case json, data
}
var accessReturnType: AccessReturnType = .data
let oauth_token = "accesskey"
let oauth_token_secret = "accesssecret"
let valid_key = "key"
let valid_secret = "key"
init() {
server = HttpServer()
server["1/requestToken"] = { request in
guard request.method == "POST" else {
return .badRequest(.text("Method must be POST"))
}
// TODO check request.headers["authorization"] for consumer key, etc...
let oauth_token = "requestkey"
let oauth_token_secret = "requestsecret"
return .ok(.text("oauth_token=\(oauth_token)&oauth_token_secret=\(oauth_token_secret)" as String) )
}
server["1/accessToken"] = { request in
guard request.method == "POST" else {
return HttpResponse.badRequest(.text("Method must be POST"))
}
// TODO check request.headers["authorization"] for consumer key, etc...
return .ok(.text("oauth_token=\(self.oauth_token)&oauth_token_secret=\(self.oauth_token_secret)" as String) )
}
/*
server["1/authorize"] = {
.OK(.HTML("You asked for " + $0.url))
}
server["/callback"] = {
.OK(.HTML("You asked for " + $0.url))
}
*/
server["2/accessToken"] = { request in
guard request.method == "POST" else {
return .badRequest(.text("Method must be POST"))
}
/*guard let autho = request.headers["authorization"] where autho == "Beared" else {
return HttpResponse.BadRequest
}*/
// TODO check body for consumer key, etc...
switch self.accessReturnType {
case .json:
return .ok(.json(["access_token":self.oauth_token] as AnyObject))
case .data:
return .ok(.text("access_token=\(self.oauth_token)" as String))
}
}
server["2/authorize"] = { request in
return .ok(HttpResponseBody.html("You asked for \(request.path)"))
}
server["2/expire"] = { request in
return HttpResponse.raw(401, "Unauthorized",["WWW-Authenticate": "Bearer realm=\"example\",error=\"invalid_token\",error_description=\"The access token expired\""], nil)
}
}
func start() throws {
try server.start(self.port)
}
func stop() {
self.server.stop()
}
}