-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.swift
More file actions
executable file
·147 lines (109 loc) · 3.65 KB
/
main.swift
File metadata and controls
executable file
·147 lines (109 loc) · 3.65 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// main.swift
// PerfectTurnstileSQLiteDemo
//
// Created by Jonathan Guthrie on 2016-10-11.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
import StORM
import SQLiteStORM
import PerfectTurnstileSQLite
import PerfectRequestLogger
//StORMdebug = true
// Used later in script for the Realm and how the user authenticates.
let pturnstile = TurnstilePerfectRealm()
// Set the connection vatiable
//connect = SQLiteConnect("./authdb")
SQLiteConnector.db = "./authdb"
RequestLogFile.location = "./http_log.txt"
// Set up the Authentication table
let auth = AuthAccount()
do {
try auth.setupTable()
} catch {
fatalError("Could not set AuthAccount's SQLite DB table. exiting")
}
// Connect the AccessTokenStore
tokenStore = AccessTokenStore()
tokenStore?.setup()
//let facebook = Facebook(clientID: "CLIENT_ID", clientSecret: "CLIENT_SECRET")
//let google = Google(clientID: "CLIENT_ID", clientSecret: "CLIENT_SECRET")
// Create HTTP server.
let server = HTTPServer()
// Register routes and handlers
let authWebRoutes = makeWebAuthRoutes()
let authJSONRoutes = makeJSONAuthRoutes("/api/v1")
// Add the routes to the server.
server.addRoutes(authWebRoutes)
server.addRoutes(authJSONRoutes)
// Adding a test route
var routes = Routes()
routes.add(method: .get, uri: "/api/v1/test", handler: AuthHandlersJSON.testHandler)
// An example route where authentication will be enforced
routes.add(method: .get, uri: "/api/v1/check", handler: {
request, response in
response.setHeader(.contentType, value: "application/json")
var resp = [String: String]()
resp["authenticated"] = "AUTHED: \(request.user.authenticated)"
resp["authDetails"] = "DETAILS: \(request.user.authDetails)"
do {
try response.setBody(json: resp)
} catch {
print(error)
}
response.completed()
})
// An example route where auth will not be enforced
routes.add(method: .get, uri: "/api/v1/nocheck", handler: {
request, response in
response.setHeader(.contentType, value: "application/json")
var resp = [String: String]()
resp["authenticated"] = "AUTHED: \(request.user.authenticated)"
resp["authDetails"] = "DETAILS: \(request.user.authDetails)"
do {
try response.setBody(json: resp)
} catch {
print(error)
}
response.completed()
})
// Add the routes to the server.
server.addRoutes(routes)
// Setup logging
let myLogger = RequestLogger()
// add routes to be checked for auth
var authenticationConfig = AuthenticationConfig()
authenticationConfig.include("/api/v1/check")
authenticationConfig.exclude("/api/v1/login")
authenticationConfig.exclude("/api/v1/register")
let authFilter = AuthFilter(authenticationConfig)
// Note that order matters when the filters are of the same priority level
server.setRequestFilters([pturnstile.requestFilter])
server.setResponseFilters([pturnstile.responseFilter])
server.setRequestFilters([(authFilter, .high)])
server.setRequestFilters([(myLogger, .high)])
server.setResponseFilters([(myLogger, .low)])
// Set a listen port of 8181
server.serverPort = 8181
// Where to serve static files from
server.documentRoot = "./webroot"
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
}