Skip to content

Commit bfa98dd

Browse files
committed
merged from develop
1 parent 622ebcd commit bfa98dd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+11915
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.wordnik.swagger.auth.service
2+
3+
import com.wordnik.swagger.auth.model._
4+
5+
import org.apache.oltu.oauth2.as.issuer.{ MD5Generator, OAuthIssuerImpl }
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
import java.util.Date
11+
import java.net.URLEncoder
12+
import java.net.URI
13+
14+
class DefaultAuthDialog extends AuthDialog with TokenCache {
15+
/**
16+
* In this sample, the scope 'anonymous' will allow access if the redirectUri
17+
* is 'localhost' and provide an AnonymousTokenRequest, which is good for 3600
18+
* seconds
19+
*/
20+
def show(clientId: String, redirectUri: String, scope: String, requestId: Option[String]) = {
21+
if(scope == "anonymous") {
22+
val url = "/oauth/login"
23+
24+
if(redirectUri.startsWith("http://localhost")) {
25+
// it's good to proceed
26+
val oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator())
27+
val accessToken = oauthIssuerImpl.accessToken()
28+
val token = AnonymousTokenResponse(3600, accessToken)
29+
tokenCache += accessToken -> TokenWrapper(new Date, token)
30+
val redirectTo = {
31+
(redirectUri.indexOf("#") match {
32+
case i: Int if(i >= 0) => redirectUri + "&"
33+
case i: Int => redirectUri + "#"
34+
}) + "access_token=" + accessToken
35+
}
36+
ApiResponseMessage(302, redirectTo)
37+
}
38+
else throw new Exception("bad redirect_uri")
39+
}
40+
else {
41+
/**
42+
* render the login dialog
43+
*/
44+
val html =
45+
<html class="js placeholder">
46+
<head>
47+
<link rel="stylesheet" href="/css/auth.css"></link>
48+
<script src="/lib/jquery-1.8.0.min.js"></script>
49+
<script src="/js/auth.js"></script>
50+
</head>
51+
<body id = "oauth-authorize" class = "oauth-page">
52+
<header>
53+
<img class="main_image" src="/images/dog.png"></img>
54+
<h1></h1>
55+
<p class="oauth_body title">
56+
<strong>Hello, <a href="#">Swagger Petstore</a>&nbsp;would like to you log in.</strong>
57+
</p>
58+
<p class="oauth_body">
59+
This is a sample OAuth2 server which supports the Implicit or Client-side flow. You can find
60+
the source code to the server <a href="https://github.com/wordnik/swagger-core/tree/master/samples/scala-oauth-authorization-server">here</a>.
61+
</p>
62+
<p class="oauth_body">
63+
Since we don't really keep track of usernames in this sample server, you can enter any non-blank username
64+
and password to simulate a successful login. An empty username or password will be treated like a
65+
login failure.
66+
</p>
67+
</header>
68+
<section class = "email_form">
69+
<form class="confirm" method="POST" id="oauth-authorize-login" action={"/oauth/login?redirect_uri=" + redirectUri}>
70+
<input name="scope" id="scope" type="hidden" value="email"></input>
71+
<input name="client_id" id="client_id" type="hidden" value="someclientid"></input>
72+
<input name="accept" id="accept" type="hidden" value="Allow"></input>
73+
<input name="request_id" id="request_id" type="hidden" value={{requestId.getOrElse("")}}></input>
74+
75+
<div class="oauth_submit initial_form">
76+
<button type="button" id="deny" class="button medium grey">
77+
<span>Cancel and go back</span>
78+
</button>
79+
<button type="button" id="allow" class="button medium blue">
80+
<span>Allow</span>
81+
</button>
82+
</div>
83+
<div class="oauth_submit secondary_form" >
84+
<label for="username">Username</label>
85+
<input name="username" type="text" placeholder="Username" value=""></input>
86+
<label for="password">Password</label>
87+
<input name="password" type="password" placeholder="Password"></input>
88+
<button type="submit" id="login" name="login" class="button medium blue">
89+
<span>Confirm</span>
90+
</button>
91+
</div>
92+
</form>
93+
</section>
94+
</body>
95+
</html>
96+
ApiResponseMessage(200, html.toString)
97+
}
98+
}
99+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Copyright 2012 Wordnik, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.wordnik.swagger.sample.data
18+
19+
import com.wordnik.swagger.sample.model.{ Tag, Category, Pet }
20+
21+
import java.util.ArrayList
22+
23+
import collection.mutable.ListBuffer
24+
import scala.collection.JavaConversions._
25+
26+
object PetData {
27+
28+
val pets: ListBuffer[Pet] = new ListBuffer[Pet]()
29+
val categories: ListBuffer[Category] = new ListBuffer[Category]()
30+
31+
categories += createCategory(1, "Dogs")
32+
categories += createCategory(2, "Cats")
33+
categories += createCategory(3, "Rabbits")
34+
categories += createCategory(4, "Lions")
35+
36+
pets += createPet(1, categories(1), "Cat 1", List("url1", "url2"), List("tag1", "tag2"), "available")
37+
pets += createPet(2, categories(1), "Cat 2", List("url1", "url2"), List("tag2", "tag3"), "available")
38+
pets += createPet(3, categories(1), "Cat 3", List("url1", "url2"), List("tag3", "tag4"), "pending")
39+
40+
pets += createPet(4, categories(0), "Dog 1", List("url1", "url2"), List("tag1", "tag2"), "available")
41+
pets += createPet(5, categories(0), "Dog 2", List("url1", "url2"), List("tag2", "tag3"), "sold")
42+
pets += createPet(6, categories(0), "Dog 3", List("url1", "url2"), List("tag3", "tag4"), "pending")
43+
44+
pets += createPet(7, categories(3), "Lion 1", List("url1", "url2"), List("tag1", "tag2"), "available")
45+
pets += createPet(8, categories(3), "Lion 2", List("url1", "url2"), List("tag2", "tag3"), "available")
46+
pets += createPet(9, categories(3), "Lion 3", List("url1", "url2"), List("tag3", "tag4"), "available")
47+
48+
pets += createPet(10, categories(2), "Rabbit 1", List("url1", "url2"), List("tag3", "tag4"), "available")
49+
50+
def getPetbyId(petId: Long): Pet = {
51+
for (pet <- pets) {
52+
if (pet.getId() == petId) {
53+
return pet
54+
}
55+
}
56+
null
57+
}
58+
59+
def deletePet(petId: Long) = {
60+
Option(getPetbyId(petId)) match {
61+
case Some(pet) => pets -= pet
62+
case _ => // do nothing
63+
}
64+
}
65+
66+
def findPetByStatus(status: String): java.util.List[Pet] = {
67+
var statues = status.split(",")
68+
var result = new java.util.ArrayList[Pet]()
69+
for (pet <- pets) {
70+
if (statues.contains(pet.getStatus())) {
71+
result.add(pet)
72+
}
73+
}
74+
result
75+
}
76+
77+
def findPetByTags(tags: String): java.util.List[Pet] = {
78+
var tagList = tags.split(",")
79+
var result = new java.util.ArrayList[Pet]()
80+
for (pet <- pets) {
81+
if (null != pet.getTags()) {
82+
for (tag <- pet.getTags()) {
83+
if (tagList.contains(tag.getName)) {
84+
result.add(pet)
85+
}
86+
}
87+
}
88+
}
89+
result
90+
}
91+
92+
def addPet(pet: Pet): Unit = {
93+
// remove any pets with same id
94+
pets --= pets.filter(p => p.getId == pet.getId)
95+
pets += pet
96+
}
97+
98+
private def createPet(id: Long, cat: Category, name: String, urls: List[String], tags: List[String], status: String): Pet = {
99+
val pet = new Pet
100+
pet.setId(id)
101+
pet.setCategory(cat)
102+
pet.setName(name)
103+
if (null != urls) {
104+
var urlObjs: java.util.List[String] = new java.util.ArrayList[String]()
105+
for (urlString <- urls) {
106+
urlObjs.add(urlString)
107+
}
108+
pet.setPhotoUrls(urlObjs)
109+
}
110+
var tagObjs: java.util.List[Tag] = new java.util.ArrayList[Tag]()
111+
var i = 0
112+
if (null != tags) {
113+
for (tagString <- tags) {
114+
i = i + 1
115+
val tag = new Tag()
116+
tag.setId(i)
117+
tag.setName(tagString)
118+
tagObjs.add(tag)
119+
}
120+
}
121+
pet.setTags(tagObjs)
122+
pet.setStatus(status)
123+
pet
124+
}
125+
126+
private def createCategory(id: Long, name: String): Category = {
127+
val category = new Category
128+
category.setId(id)
129+
category.setName(name)
130+
category
131+
}
132+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright 2012 Wordnik, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.wordnik.swagger.sample.data
18+
19+
import collection.mutable.ListBuffer
20+
import com.wordnik.swagger.sample.model.{ Order }
21+
import java.util.Date
22+
23+
object StoreData {
24+
val orders: ListBuffer[Order] = new ListBuffer[Order]()
25+
orders += createOrder(1, 1, 2, new Date(), "placed")
26+
orders += createOrder(2, 1, 2, new Date(), "delivered")
27+
orders += createOrder(3, 2, 2, new Date(), "placed")
28+
orders += createOrder(4, 2, 2, new Date(), "delivered")
29+
orders += createOrder(5, 3, 2, new Date(), "placed")
30+
31+
def findOrderById(orderId: Long): Order = {
32+
for (order <- orders) {
33+
if (order.getId() == orderId) {
34+
return order
35+
}
36+
}
37+
null
38+
}
39+
40+
def placeOrder(order: Order): Unit = {
41+
// remove any pets with same id
42+
orders --= orders.filter(o => o.getId == order.getId)
43+
orders += order
44+
}
45+
46+
def deleteOrder(orderId: Long): Unit = {
47+
orders --= orders.filter(o => o.getId == orderId)
48+
}
49+
50+
private def createOrder(id: Long, petId: Long, quantity: Int, shipDate: Date, status: String): Order = {
51+
val order = new Order()
52+
order.setId(id)
53+
order.setPetId(petId)
54+
order.setQuantity(quantity)
55+
order.setShipDate(shipDate)
56+
order.setStatus(status)
57+
order
58+
}
59+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright 2012 Wordnik, Inc.
3+
*
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.wordnik.swagger.sample.data
19+
20+
import com.wordnik.swagger.sample.model.{ Category, User, Pet }
21+
22+
import collection.mutable.ListBuffer
23+
24+
object UserData {
25+
val users: ListBuffer[User] = new ListBuffer[User]()
26+
27+
users += createUser(1, "user1", "first name 1", "last name 1", "[email protected]", "123-456-7890", 1)
28+
users += createUser(2, "user2", "first name 2", "last name 2", "[email protected]", "123-456-7890", 2)
29+
users += createUser(3, "user3", "first name 3", "last name 3", "[email protected]", "123-456-7890", 3)
30+
users += createUser(4, "user4", "first name 4", "last name 4", "[email protected]", "123-456-7890", 1)
31+
users += createUser(5, "user5", "first name 5", "last name 5", "[email protected]", "123-456-7890", 2)
32+
users += createUser(6, "user6", "first name 6", "last name 6", "[email protected]", "123-456-7890", 3)
33+
users += createUser(7, "user7", "first name 7", "last name 7", "[email protected]", "123-456-7890", 1)
34+
users += createUser(8, "user8", "first name 8", "last name 8", "[email protected]", "123-456-7890", 2)
35+
users += createUser(9, "user9", "first name 9", "last name 9", "[email protected]", "123-456-7890", 3)
36+
users += createUser(10, "user10", "first name 10", "last name 10", "[email protected]", "123-456-7890", 1)
37+
users += createUser(11, "user?10", "first name ?10", "last name ?10", "[email protected]", "123-456-7890", 1)
38+
39+
def findUserByName(username: String): User = {
40+
for (user <- users) {
41+
if (user.getUsername() == username) {
42+
return user
43+
}
44+
}
45+
null
46+
}
47+
48+
def addUser(user: User): Unit = {
49+
users --= users.filter(u => u.getId == user.getId)
50+
users += user
51+
}
52+
53+
def removeUser(username: String): Unit = {
54+
for (user <- users) {
55+
if (user.getUsername() == username) {
56+
users -= user
57+
}
58+
}
59+
}
60+
61+
private def createUser(id: Long, username: String, firstName: String, lastName: String, email: String, phone: String, userStatus: Int): User = {
62+
var user = new User
63+
user.setId(id)
64+
user.setUsername(username)
65+
user.setFirstName(firstName)
66+
user.setLastName(lastName)
67+
user.setEmail(email)
68+
user.setPassword("XXXXXXXXXXX")
69+
user.setPhone(phone)
70+
user.setUserStatus(userStatus)
71+
user
72+
}
73+
}

0 commit comments

Comments
 (0)