This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 566
/
SecurityDirectivesSpec.scala
161 lines (144 loc) · 6.94 KB
/
SecurityDirectivesSpec.scala
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright © 2011-2015 the spray project <http://spray.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spray.routing
import scala.concurrent.Future
import akka.event.NoLogging
import spray.routing.authentication._
import spray.http._
import HttpHeaders._
import AuthenticationFailedRejection._
class SecurityDirectivesSpec extends RoutingSpec {
val dontAuth = BasicAuth(UserPassAuthenticator[BasicUserContext](_ ⇒ Future.successful(None)), "Realm")
val challenge = `WWW-Authenticate`(HttpChallenge("Basic", "Realm"))
val doAuth = BasicAuth(UserPassAuthenticator[BasicUserContext] { userPassOption ⇒
Future.successful(Some(BasicUserContext(userPassOption.get.user)))
}, "Realm")
"the 'authenticate(BasicAuth())' directive" should {
"reject requests without Authorization header with an AuthenticationFailedRejection" in {
Get() ~> {
authenticate(dontAuth) { echoComplete }
} ~> check { rejection === AuthenticationFailedRejection(CredentialsMissing, List(challenge)) }
}
"reject unauthenticated requests with Authorization header with an AuthenticationFailedRejection" in {
Get() ~> Authorization(BasicHttpCredentials("Bob", "")) ~> {
authenticate(dontAuth) { echoComplete }
} ~> check { rejection === AuthenticationFailedRejection(CredentialsRejected, List(challenge)) }
}
"reject requests with illegal Authorization header with 401" in {
Get() ~> RawHeader("Authorization", "bob alice") ~> handleRejections(RejectionHandler.Default) {
authenticate(dontAuth) { echoComplete }
} ~> check {
status === StatusCodes.Unauthorized and
responseAs[String] === "The resource requires authentication, which was not supplied with the request"
}
}
"extract the object representing the user identity created by successful authentication" in {
Get() ~> Authorization(BasicHttpCredentials("Alice", "")) ~> {
authenticate(doAuth) { echoComplete }
} ~> check { responseAs[String] === "BasicUserContext(Alice)" }
}
"properly handle exceptions thrown in its inner route" in {
object TestException extends spray.util.SingletonException
Get() ~> Authorization(BasicHttpCredentials("Alice", "")) ~> {
handleExceptions(ExceptionHandler.default) {
authenticate(doAuth) { _ ⇒ throw TestException }
}
} ~> check { status === StatusCodes.InternalServerError }
}
}
"the 'authenticate(<ContextAuthenticator>)' directive" should {
case object AuthenticationRejection extends Rejection
val myAuthenticator: ContextAuthenticator[Int] = ctx ⇒ Future {
Either.cond(ctx.request.uri.authority.host == Uri.NamedHost("spray.io"), 42, AuthenticationRejection)
}
"reject requests not satisfying the filter condition" in {
Get() ~> authenticate(myAuthenticator) { echoComplete } ~>
check { rejection === AuthenticationRejection }
}
"pass on the authenticator extraction if the filter conditions is met" in {
Get() ~> Host("spray.io") ~> authenticate(myAuthenticator) { echoComplete } ~>
check { responseAs[String] === "42" }
}
}
"the 'authenticate(<Future>)' directive" should {
var i = 0
def nextInt() = { i += 1; i }
def myAuthenticator: Future[Authentication[Int]] = Future.successful(Right(nextInt()))
val route = authenticate(myAuthenticator) { echoComplete }
"pass on the authenticator extraction if the filter conditions is met" in {
Get() ~> Host("spray.io") ~> route ~>
check { responseAs[String] === "1" }
Get() ~> Host("spray.io") ~> route ~>
check { responseAs[String] === "2" }
}
}
"the 'optionalAuthenticate(BasicAuth())' directive" should {
"extract None from requests without Authorization header" in {
Get() ~> {
optionalAuthenticate(dontAuth) { echoComplete }
} ~> check { responseAs[String] === "None" }
}
"extract None from requests with Authorization header using a different scheme" in {
Get() ~> RawHeader("Authorization", "bob alice") ~> {
optionalAuthenticate(dontAuth) { echoComplete }
} ~> check { responseAs[String] === "None" }
}
"reject unauthenticated requests with Authorization header with an AuthenticationFailedRejection" in {
Get() ~> Authorization(BasicHttpCredentials("Bob", "")) ~> {
optionalAuthenticate(dontAuth) { echoComplete }
} ~> check { rejection === AuthenticationFailedRejection(CredentialsRejected, List(challenge)) }
}
"extract Some(object) representing the user identity created by successful authentication" in {
Get() ~> Authorization(BasicHttpCredentials("Alice", "")) ~> {
optionalAuthenticate(doAuth) { echoComplete }
} ~> check { responseAs[String] === "Some(BasicUserContext(Alice))" }
}
"properly handle exceptions thrown in its inner route" in {
object TestException extends spray.util.SingletonException
Get() ~> Authorization(BasicHttpCredentials("Alice", "")) ~> {
handleExceptions(ExceptionHandler.default) {
optionalAuthenticate(doAuth) { _ ⇒ throw TestException }
}
} ~> check { status === StatusCodes.InternalServerError }
}
}
"the 'optionalAuthenticate(<ContextAuthenticator>)' directive" should {
case object AuthenticationRejection extends Rejection
val myAuthenticator: ContextAuthenticator[Int] = ctx ⇒ Future {
Either.cond(ctx.request.uri.authority.host == Uri.NamedHost("spray.io"), 42, AuthenticationRejection)
}
"reject requests not satisfying the filter condition" in {
Get() ~> optionalAuthenticate(myAuthenticator) { echoComplete } ~>
check { rejection === AuthenticationRejection }
}
"pass on the authenticator extraction if the filter conditions is met" in {
Get() ~> Host("spray.io") ~> optionalAuthenticate(myAuthenticator) { echoComplete } ~>
check { responseAs[String] === "Some(42)" }
}
}
"the 'optionalAuthenticate(<Future>)' directive" should {
var i = 0
def nextInt() = { i += 1; i }
def myAuthenticator: Future[Authentication[Int]] = Future.successful(Right(nextInt()))
val route = optionalAuthenticate(myAuthenticator) { echoComplete }
"pass on the authenticator extraction if the filter conditions is met" in {
Get() ~> Host("spray.io") ~> route ~>
check { responseAs[String] === "Some(1)" }
Get() ~> Host("spray.io") ~> route ~>
check { responseAs[String] === "Some(2)" }
}
}
}