Skip to content

Commit ab4da9a

Browse files
authored
remove commons-codec from dependency (#104)
1 parent 56ed28a commit ab4da9a

File tree

2 files changed

+28
-40
lines changed

2 files changed

+28
-40
lines changed

project/Build.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ object ScalaOAuth2Build extends Build {
1212
val _crossScalaVersions = Seq("2.11.8")
1313

1414
val commonDependenciesInTestScope = Seq(
15-
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
15+
"org.scalatest" %% "scalatest" % "2.2.4" % "test",
16+
"ch.qos.logback" % "logback-classic" % "1.1.7" % "test"
1617
)
1718

1819
lazy val scalaOAuth2ProviderSettings =
@@ -46,9 +47,7 @@ object ScalaOAuth2Build extends Build {
4647
settings = scalaOAuth2ProviderSettings ++ Seq(
4748
name := "scala-oauth2-core",
4849
description := "OAuth 2.0 server-side implementation written in Scala",
49-
libraryDependencies ++= Seq(
50-
"commons-codec" % "commons-codec" % "1.8"
51-
) ++ commonDependenciesInTestScope
50+
libraryDependencies ++= commonDependenciesInTestScope
5251
)
5352
)
5453

scala-oauth2-core/src/main/scala/scalaoauth2/provider/AuthorizationRequest.scala

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,39 @@
11
package scalaoauth2.provider
22

3-
import org.apache.commons.codec.binary.Base64
3+
import java.util.Base64
4+
5+
import scala.util.Try
46

57
case class ClientCredential(clientId: String, clientSecret: Option[String])
68

79
class AuthorizationRequest(headers: Map[String, Seq[String]], params: Map[String, Seq[String]]) extends RequestBase(headers, params) {
810

9-
/**
10-
* Returns grant_type.
11-
*
12-
* OAuth defines four grant types:
13-
* authorization code
14-
* implicit
15-
* resource owner password credentials, and client credentials.
16-
*
17-
* @return grant_type
18-
*/
19-
def grantType: String = requireParam("grant_type")
20-
21-
/**
22-
* Returns scope.
23-
*
24-
* @return scope
25-
*/
2611
def scope: Option[String] = param("scope")
2712

28-
lazy val clientCredential: Option[ClientCredential] = {
29-
header("Authorization").flatMap {
30-
"""^\s*Basic\s+(.+?)\s*$""".r.findFirstMatchIn
31-
} match {
32-
case Some(matcher) =>
33-
val authorization = matcher.group(1)
34-
val decoded = new String(Base64.decodeBase64(authorization.getBytes), "UTF-8")
35-
if (decoded.indexOf(':') > 0) {
36-
decoded.split(":", 2) match {
37-
case Array(clientId, clientSecret) => Some(ClientCredential(clientId, if (clientSecret == "") None else Some(clientSecret)))
38-
case Array(clientId) => Some(ClientCredential(clientId, None))
39-
}
40-
} else {
13+
def grantType: String = requireParam("grant_type")
14+
15+
lazy val clientCredential: Option[ClientCredential] =
16+
findAuthorization
17+
.flatMap(clientCredentialByAuthorization)
18+
.orElse(clientCredentialByParam)
19+
20+
private def findAuthorization = for {
21+
authorization <- header("Authorization")
22+
matcher <- """^\s*Basic\s+(.+?)\s*$""".r.findFirstMatchIn(authorization)
23+
} yield matcher.group(1)
24+
25+
private def clientCredentialByAuthorization(s: String) =
26+
Try(new String(Base64.getDecoder.decode(s), "UTF-8"))
27+
.map(_.split(":", 2))
28+
.getOrElse(Array.empty) match {
29+
case Array(clientId, clientSecret) =>
30+
Some(ClientCredential(clientId, if (clientSecret.isEmpty) None else Some(clientSecret)))
31+
case _ =>
4132
None
42-
}
43-
case _ => param("client_id").map { clientId =>
44-
ClientCredential(clientId, param("client_secret"))
4533
}
46-
}
47-
}
34+
35+
private def clientCredentialByParam = param("client_id").map(ClientCredential(_, param("client_secret")))
36+
4837
}
4938

5039
case class RefreshTokenRequest(request: AuthorizationRequest) extends AuthorizationRequest(request.headers, request.params) {

0 commit comments

Comments
 (0)