Merge pull request #263 from wordnik/basic-auth

Adds Basic auth as an authentication mechanism
This commit is contained in:
Tony Tam 2014-09-20 08:43:30 -07:00
commit 2fd29d74f2
4 changed files with 17 additions and 12 deletions

View File

@ -21,6 +21,7 @@ libraryDependencies ++= Seq(
"org.json4s" %% "json4s-jackson" % "3.2.10",
"io.backchat.inflector" %% "scala-inflector" % "1.3.5",
"commons-io" % "commons-io" % "2.3",
"net.iharder" % "base64" % "2.3.8",
"ch.qos.logback" % "logback-classic" % "1.0.13" % "provided",
"org.rogach" %% "scallop" % "0.9.5",
"junit" % "junit" % "4.11" % "test",

View File

@ -1 +1 @@
sbt.version=0.13.0
sbt.version=0.13.5

View File

@ -28,6 +28,7 @@ import com.wordnik.swagger.util.ValidationException
import java.io.{ File, FileWriter }
import net.iharder.Base64
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization.write
@ -187,18 +188,20 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
}
def authenticate(apiKey: Option[String]): Option[ApiKeyValue] = {
Option(System.getProperty("header")) match {
case Some(e) => {
val headerAuth = sys.props.get("header") map { e =>
// this is ugly and will be replaced with proper arg parsing like in ScalaAsyncClientGenerator soon
val authInfo = e.split(":")
Some(ApiKeyValue(authInfo(0), "header", authInfo(1)))
ApiKeyValue(authInfo(0), "header", authInfo(1))
}
case _ => {
apiKey.map{ key =>
Some(ApiKeyValue("api_key", "query", key))
}.getOrElse(None)
val basicAuth = sys.props.get("auth.basic") map { e =>
val creds = if (e.contains(":")) Base64.encodeBytes(e.getBytes) else e
ApiKeyValue("Authorization", "header", s"Basic $creds")
}
val apiKeyAuth = apiKey map { key =>
ApiKeyValue("api_key", "query", key)
}
headerAuth orElse basicAuth orElse apiKeyAuth
}
def extractApiOperations(apiListings: List[ApiListing], allModels: HashMap[String, Model] )(implicit basePath:String) = {

View File

@ -44,3 +44,4 @@ case class AuthorizationCodeGrant(
trait AuthorizationValue
case class ApiKeyValue(keyName: String, passAs: String, value: String) extends AuthorizationValue