Skip to content

Commit 6ad2092

Browse files
committed
Initial commit
0 parents  commit 6ad2092

File tree

11 files changed

+538
-0
lines changed

11 files changed

+538
-0
lines changed

.gitignore

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Xcode
2+
#
3+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4+
5+
## Build generated
6+
build/
7+
DerivedData
8+
9+
## Various settings
10+
*.pbxuser
11+
!default.pbxuser
12+
*.mode1v3
13+
!default.mode1v3
14+
*.mode2v3
15+
!default.mode2v3
16+
*.perspectivev3
17+
!default.perspectivev3
18+
xcuserdata
19+
20+
## Other
21+
*.xccheckout
22+
*.moved-aside
23+
*.xcuserstate
24+
*.xcscmblueprint
25+
26+
## Obj-C/Swift specific
27+
*.hmap
28+
*.ipa
29+
30+
## Playgrounds
31+
timeline.xctimeline
32+
playground.xcworkspace
33+
34+
# Swift Package Manager
35+
#
36+
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
37+
# Packages/
38+
.build/
39+
40+
# CocoaPods
41+
#
42+
# We recommend against adding the Pods directory to your .gitignore. However
43+
# you should judge for yourself, the pros and cons are mentioned at:
44+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
45+
#
46+
# Pods/
47+
48+
# Carthage
49+
#
50+
# Add this line if you want to avoid checking in source code from Carthage dependencies.
51+
# Carthage/Checkouts
52+
53+
Carthage/Build
54+
55+
# fastlane
56+
#
57+
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
58+
# screenshots whenever they are needed.
59+
# For more information about the recommended setup visit:
60+
# https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md
61+
62+
fastlane/report.xml
63+
fastlane/screenshots
64+
65+
Packages/
66+
*.xcodeproj
67+
buildlinux
68+
/.packages_lin
69+
/.build_lin
70+
Package.swift.orig

Package.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Generated automatically by Perfect Assistant Application
2+
// Date: 2016-12-08 17:47:11 +0000
3+
import PackageDescription
4+
let package = Package(
5+
name: "PerfectTurnstileMySQLDemo",
6+
targets: [],
7+
dependencies: [
8+
.Package(url: "https://github.com/PerfectlySoft/Perfect-Turnstile-MySQL.git", majorVersion: 1, minor: 0),
9+
.Package(url: "https://github.com/PerfectlySoft/Perfect-RequestLogger.git", majorVersion: 0, minor: 5),
10+
]
11+
)

README.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Perfect Turnstile with MySQL
2+
3+
<p align="center">
4+
<a href="http://perfect.org/get-involved.html" target="_blank">
5+
<img src="http://perfect.org/assets/github/perfect_github_2_0_0.jpg" alt="Get Involed with Perfect!" width="854" />
6+
</a>
7+
</p>
8+
9+
<p align="center">
10+
<a href="https://github.com/PerfectlySoft/Perfect" target="_blank">
11+
<img src="http://www.perfect.org/github/Perfect_GH_button_1_Star.jpg" alt="Star Perfect On Github" />
12+
</a>
13+
<a href="http://stackoverflow.com/questions/tagged/perfect" target="_blank">
14+
<img src="http://www.perfect.org/github/perfect_gh_button_2_SO.jpg" alt="Stack Overflow" />
15+
</a>
16+
<a href="https://twitter.com/perfectlysoft" target="_blank">
17+
<img src="http://www.perfect.org/github/Perfect_GH_button_3_twit.jpg" alt="Follow Perfect on Twitter" />
18+
</a>
19+
<a href="http://perfect.ly" target="_blank">
20+
<img src="http://www.perfect.org/github/Perfect_GH_button_4_slack.jpg" alt="Join the Perfect Slack" />
21+
</a>
22+
</p>
23+
24+
<p align="center">
25+
<a href="https://developer.apple.com/swift/" target="_blank">
26+
<img src="https://img.shields.io/badge/Swift-3.0-orange.svg?style=flat" alt="Swift 3.0">
27+
</a>
28+
<a href="https://developer.apple.com/swift/" target="_blank">
29+
<img src="https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat" alt="Platforms OS X | Linux">
30+
</a>
31+
<a href="http://perfect.org/licensing.html" target="_blank">
32+
<img src="https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat" alt="License Apache">
33+
</a>
34+
<a href="http://twitter.com/PerfectlySoft" target="_blank">
35+
<img src="https://img.shields.io/badge/[email protected]?style=flat" alt="PerfectlySoft Twitter">
36+
</a>
37+
<a href="http://perfect.ly" target="_blank">
38+
<img src="http://perfect.ly/badge.svg" alt="Slack Status">
39+
</a>
40+
</p>
41+
42+
43+
This example demonstrates the integration Stormpath's Turnstile authentication system with Perfect and a MySQL ORM.
44+
45+
The library can be found at [https://github.com/PerfectlySoft/Perfect-Turnstile-MySQL](https://github.com/PerfectlySoft/Perfect-Turnstile-MySQL)
46+
47+
This package builds with Swift Package Manager and is part of the [Perfect](https://github.com/PerfectlySoft/Perfect) project.
48+
49+
Ensure you have installed Xcode 8.0 or later.
50+
51+
## Build Notes
52+
53+
### macOS
54+
55+
Requires the use of Homebrew’s MySQL.
56+
57+
```
58+
brew install mysql
59+
```
60+
61+
If you need Homebrew, you can install it with:
62+
63+
```
64+
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
65+
```
66+
67+
Unfortunately, at this point in time you will need to edit the mysqlclient.pc file located here:
68+
69+
```
70+
/usr/local/lib/pkgconfig/mysqlclient.pc
71+
```
72+
73+
Remove the occurrence of "-fno-omit-frame-pointer". This file is read-only by default so you will need to change that first.
74+
75+
### Linux
76+
77+
Ensure that you have installed libmysqlclient-dev for MySQL version 5.6 or greater:
78+
79+
```
80+
sudo apt-get install libmysqlclient-dev
81+
```
82+
83+
Please note that Ubuntu 14 defaults to including a version of MySQL client which will not compile with this package. Install MySQL client version 5.6 or greater manually.
84+
85+
86+
## Setup - Xcode 8
87+
88+
* Check out or download the project;
89+
* In terminal, navigate to the directory and execute
90+
91+
```
92+
swift package generate-xcodeproj
93+
```
94+
95+
* Open `PerfectTurnstile MySQL Demo.xcodeproj`
96+
97+
To run this project from Xcode, edit the Scheme, Under "Options" for "run", check "Use custom working directory" and choose the project's working directory. After doing this, the project can be run from within Xcode.
98+
99+
## Setup - Terminal
100+
101+
* Check out or download the project;
102+
* In terminal, navigate to the directory
103+
* Execute `swift build`
104+
* Once the project has compiled, execute `./.build/debug/PerfectTurnstile MySQL Demo`
105+
106+
```
107+
[INFO] Starting HTTP server on 0.0.0.0:8181 with document root ./webroot
108+
```
109+
110+
111+
## Included JSON Routes
112+
113+
The framework includes certain basic routes:
114+
115+
```
116+
POST /api/v1/login (with username & password form elements)
117+
POST /api/v1/register (with username & password form elements)
118+
GET /api/v1/logout
119+
```
120+
121+
## Included Routes for Browser
122+
123+
The following routes are available for browser testing:
124+
125+
```
126+
http://localhost:8181
127+
http://localhost:8181/login
128+
http://localhost:8181/register
129+
```
130+
131+
These routes are using Mustache files in the webroot directory.
132+

Sources/main.swift

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//
2+
// main.swift
3+
// PerfectTurnstileSQLiteDemo
4+
//
5+
// Created by Jonathan Guthrie on 2016-10-11.
6+
// Copyright (C) 2015 PerfectlySoft, Inc.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This source file is part of the Perfect.org open source project
11+
//
12+
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
13+
// Licensed under Apache License v2.0
14+
//
15+
// See http://perfect.org/licensing.html for license information
16+
//
17+
//===----------------------------------------------------------------------===//
18+
//
19+
20+
import PerfectLib
21+
import PerfectHTTP
22+
import PerfectHTTPServer
23+
24+
import StORM
25+
import MySQLStORM
26+
import PerfectTurnstileMySQL
27+
import PerfectRequestLogger
28+
import TurnstilePerfect
29+
30+
31+
StORMdebug = true
32+
33+
// Used later in script for the Realm and how the user authenticates.
34+
let pturnstile = TurnstilePerfectRealm()
35+
36+
37+
MySQLConnector.host = "localhost"
38+
MySQLConnector.username = "perfect"
39+
MySQLConnector.password = "perfect"
40+
MySQLConnector.database = "perfect_testing"
41+
MySQLConnector.port = 3306
42+
43+
44+
45+
// Set up the Authentication table
46+
let auth = AuthAccount()
47+
try? auth.setup()
48+
49+
// Connect the AccessTokenStore
50+
tokenStore = AccessTokenStore()
51+
try? tokenStore?.setup()
52+
53+
//let facebook = Facebook(clientID: "CLIENT_ID", clientSecret: "CLIENT_SECRET")
54+
//let google = Google(clientID: "CLIENT_ID", clientSecret: "CLIENT_SECRET")
55+
56+
// Create HTTP server.
57+
let server = HTTPServer()
58+
59+
// Register routes and handlers
60+
let authWebRoutes = makeWebAuthRoutes()
61+
let authJSONRoutes = makeJSONAuthRoutes("/api/v1")
62+
63+
// Add the routes to the server.
64+
server.addRoutes(authWebRoutes)
65+
server.addRoutes(authJSONRoutes)
66+
67+
// Adding a test route
68+
var routes = Routes()
69+
routes.add(method: .get, uri: "/api/v1/test", handler: AuthHandlersJSON.testHandler)
70+
71+
72+
73+
74+
// An example route where authentication will be enforced
75+
routes.add(method: .get, uri: "/api/v1/check", handler: {
76+
request, response in
77+
response.setHeader(.contentType, value: "application/json")
78+
79+
var resp = [String: String]()
80+
resp["authenticated"] = "AUTHED: \(request.user.authenticated)"
81+
resp["authDetails"] = "DETAILS: \(request.user.authDetails)"
82+
83+
do {
84+
try response.setBody(json: resp)
85+
} catch {
86+
print(error)
87+
}
88+
response.completed()
89+
})
90+
91+
92+
// An example route where auth will not be enforced
93+
routes.add(method: .get, uri: "/api/v1/nocheck", handler: {
94+
request, response in
95+
response.setHeader(.contentType, value: "application/json")
96+
97+
var resp = [String: String]()
98+
resp["authenticated"] = "AUTHED: \(request.user.authenticated)"
99+
resp["authDetails"] = "DETAILS: \(request.user.authDetails)"
100+
101+
do {
102+
try response.setBody(json: resp)
103+
} catch {
104+
print(error)
105+
}
106+
response.completed()
107+
})
108+
109+
110+
111+
// Add the routes to the server.
112+
server.addRoutes(routes)
113+
114+
115+
// Setup logging
116+
let myLogger = RequestLogger()
117+
118+
// add routes to be checked for auth
119+
var authenticationConfig = AuthenticationConfig()
120+
authenticationConfig.include("/api/v1/check")
121+
authenticationConfig.exclude("/api/v1/login")
122+
authenticationConfig.exclude("/api/v1/register")
123+
124+
let authFilter = AuthFilter(authenticationConfig)
125+
126+
// Note that order matters when the filters are of the same priority level
127+
server.setRequestFilters([pturnstile.requestFilter])
128+
server.setResponseFilters([pturnstile.responseFilter])
129+
130+
server.setRequestFilters([(authFilter, .high)])
131+
132+
server.setRequestFilters([(myLogger, .high)])
133+
server.setResponseFilters([(myLogger, .low)])
134+
135+
// Set a listen port of 8181
136+
server.serverPort = 8181
137+
138+
// Where to serve static files from
139+
server.documentRoot = "./webroot"
140+
141+
do {
142+
// Launch the HTTP server.
143+
try server.start()
144+
} catch PerfectError.networkError(let err, let msg) {
145+
print("Network error thrown: \(err) \(msg)")
146+
}

0 commit comments

Comments
 (0)