Skip to content

Commit ccc6b38

Browse files
committed
added filter
1 parent a89b3c3 commit ccc6b38

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.wordnik.swagger.sample.util
2+
3+
import com.wordnik.swagger.model._
4+
import com.wordnik.swagger.core.filter.SwaggerSpecFilter
5+
6+
import javax.servlet.ServletConfig
7+
import javax.servlet.http.HttpServlet
8+
9+
class SecurityFilter extends SwaggerSpecFilter {
10+
def isOperationAllowed(operation: Operation, api: ApiDescription, params: java.util.Map[String, java.util.List[String]], cookies: java.util.Map[String, String], headers: java.util.Map[String, java.util.List[String]]): Boolean = {
11+
checkKey(params, headers) match {
12+
case true => true
13+
case false => {
14+
if(operation.method == "GET" && api.path.indexOf("/store") == -1) true
15+
else false
16+
}
17+
}
18+
}
19+
20+
def isParamAllowed(parameter: Parameter, operation: Operation, api: ApiDescription, params: java.util.Map[String, java.util.List[String]], cookies: java.util.Map[String, String], headers: java.util.Map[String, java.util.List[String]]): Boolean = {
21+
val isAuthorized = checkKey(params, headers)
22+
if(parameter.paramAccess == Some("internal") && !isAuthorized) false
23+
else true
24+
}
25+
26+
def checkKey(params: java.util.Map[String, java.util.List[String]], headers: java.util.Map[String, java.util.List[String]]): Boolean = {
27+
val apiKey = params.containsKey("api_key") match {
28+
case true => Some(params.get("api_key").get(0))
29+
case _ => {
30+
headers.containsKey("api_key") match {
31+
case true => Some(headers.get("api_key").get(0))
32+
case _ => None
33+
}
34+
}
35+
}
36+
37+
apiKey match {
38+
case Some(key) if(key == "special-key") => true
39+
case _ => false
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)