adds basic auth support

This commit is contained in:
Ivan Porto Carrero 2014-09-16 20:41:32 -07:00
parent 44a73c32fa
commit ed86bfb79b
2 changed files with 16 additions and 11 deletions

View File

@ -16,6 +16,8 @@
package com.wordnik.swagger.codegen package com.wordnik.swagger.codegen
import java.util.Base64
import com.wordnik.swagger.codegen._ import com.wordnik.swagger.codegen._
import com.wordnik.swagger.codegen.util._ import com.wordnik.swagger.codegen.util._
import com.wordnik.swagger.codegen.language.CodegenConfig import com.wordnik.swagger.codegen.language.CodegenConfig
@ -187,18 +189,20 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
} }
def authenticate(apiKey: Option[String]): Option[ApiKeyValue] = { def authenticate(apiKey: Option[String]): Option[ApiKeyValue] = {
Option(System.getProperty("header")) match { val headerAuth = sys.props.get("header") map { e =>
case Some(e) => { // this is ugly and will be replaced with proper arg parsing like in ScalaAsyncClientGenerator soon
// this is ugly and will be replaced with proper arg parsing like in ScalaAsyncClientGenerator soon val authInfo = e.split(":")
val authInfo = e.split(":") ApiKeyValue(authInfo(0), "header", authInfo(1))
Some(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.getEncoder.encodeToString(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) = { def extractApiOperations(apiListings: List[ApiListing], allModels: HashMap[String, Model] )(implicit basePath:String) = {

View File

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