akka.http.scaladsl.model.headers.BasicHttpCredentials Scala Examples
The following examples show how to use akka.http.scaladsl.model.headers.BasicHttpCredentials.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: HTTPRequestAuth.scala From skuber with Apache License 2.0 | 6 votes |
package skuber.api.security import akka.http.scaladsl.model.{HttpHeader, HttpRequest} import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials, OAuth2BearerToken} import skuber.api.client._ object HTTPRequestAuth { def addAuth(request: HttpRequest, auth: AuthInfo) : HttpRequest = { getAuthHeader(auth).map(request.addHeader).getOrElse(request) } def getAuthHeader(auth: AuthInfo) : Option[HttpHeader] = { auth match { case NoAuth | _: CertAuth => None case BasicAuth(user, password) => Some(Authorization(BasicHttpCredentials(user,password))) case auth: AccessTokenAuth => Some(Authorization(OAuth2BearerToken(auth.accessToken))) } } }
Example 2
package org.apache.openwhisk.standalone import akka.Done import akka.actor.ActorSystem import akka.http.scaladsl.model.Uri.Query import akka.http.scaladsl.model.headers.{Accept, Authorization, BasicHttpCredentials} import akka.http.scaladsl.model.{HttpHeader, HttpMethods, MediaTypes, Uri} import org.apache.openwhisk.core.database.PutException import org.apache.openwhisk.http.PoolingRestClient import spray.json._ import scala.concurrent.{ExecutionContext, Future} class Wsk(host: String, port: Int, authKey: String)(implicit system: ActorSystem) extends DefaultJsonProtocol { import PoolingRestClient._ private implicit val ec: ExecutionContext = system.dispatcher private val client = new PoolingRestClient("http", host, port, 10) private val baseHeaders: List[HttpHeader] = { val Array(username, password) = authKey.split(':') List(Authorization(BasicHttpCredentials(username, password)), Accept(MediaTypes.`application/json`)) } def updatePgAction(name: String, content: String): Future[Done] = { val js = actionJson(name, content) val params = Map("overwrite" -> "true") val uri = Uri(s"/api/v1/namespaces/_/actions/$name").withQuery(Query(params)) client.requestJson[JsObject](mkJsonRequest(HttpMethods.PUT, uri, js, baseHeaders)).map { case Right(_) => Done case Left(status) => throw PutException(s"Error creating action $name " + status) } } private def actionJson(name: String, code: String) = { s"""{ | "namespace": "_", | "name": "$name", | "exec": { | "kind": "nodejs:default", | "code": ${quote(code)} | }, | "annotations": [{ | "key": "provide-api-key", | "value": true | }, { | "key": "web-export", | "value": true | }, { | "key": "raw-http", | "value": false | }, { | "key": "final", | "value": true | }], | "parameters": [{ | "key": "__ignore_certs", | "value": true | }] |}""".stripMargin.parseJson.asJsObject } private def quote(code: String) = { JsString(code).compactPrint } }
Example 3
Source File: RouteLogging.scala From akka-ddd-cqrs-es-example with MIT License | 5 votes |
package com.github.j5ik2o.bank.adaptor.controller import akka.event.Logging import akka.http.scaladsl.model.headers.{ Authorization, BasicHttpCredentials } import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.http.scaladsl.server.{ Directive0, RouteResult } import akka.http.scaladsl.server.directives.{ DebuggingDirectives, LogEntry } trait RequestFormatter { def formatRequest(request: HttpRequest): LogEntry } trait RequestResultFormatter { def formatRequestResponse(request: HttpRequest): RouteResult => Option[LogEntry] } object DefaultRequestLoggingFormatter { implicit object requestFormatter extends RequestFormatter { override def formatRequest(request: HttpRequest): LogEntry = { LogEntry(s"${formatRequestToString(request)}/", Logging.InfoLevel) } } implicit object requestResultFormatter extends RequestResultFormatter { override def formatRequestResponse(request: HttpRequest): (RouteResult) => Option[LogEntry] = { case RouteResult.Complete(response) => val req = formatRequestToString(request) val res = formatResponseToString(response) Some(LogEntry(s"$req/$res", Logging.InfoLevel)) case _ => val req = formatRequestToString(request) Some(LogEntry(req, Logging.InfoLevel)) } } private def formatRequestToString(request: HttpRequest): String = { val protocol = request.protocol.value val method = request.method.name() val path = request.uri.toString() val headers = request.headers .collect { case Authorization(_: BasicHttpCredentials) => "authorization:Basic ***" case Authorization(_) => "authorization:***" case h => s"'${h.lowercaseName()}':'${h.value()}'" } .mkString(", ") s"$protocol $method $path [$headers]" } private def formatResponseToString(request: HttpResponse): String = { val status = request.status.value val headers = request.headers.map(h => s"'${h.lowercaseName()}':'${h.value()}'").mkString(", ") s"$status [$headers]" } } class RouteLogging()(implicit requestFormatter: RequestFormatter, requestResultFormatter: RequestResultFormatter) { val httpLogRequest: Directive0 = DebuggingDirectives.logRequest(requestFormatter.formatRequest _) val httpLogRequestResult: Directive0 = DebuggingDirectives.logRequestResult(requestResultFormatter.formatRequestResponse _) } object RouteLogging { def apply()(implicit requestFormatter: RequestFormatter, requestResultFormatter: RequestResultFormatter): RouteLogging = new RouteLogging() val default: RouteLogging = new RouteLogging()(DefaultRequestLoggingFormatter.requestFormatter, DefaultRequestLoggingFormatter.requestResultFormatter) }
Example 4
Source File: BasicAuthenticationExtension.scala From scruid with Apache License 2.0 | 5 votes |
package ing.wbaa.druid.auth.basic import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.model.headers.{ Authorization, BasicHttpCredentials } import com.typesafe.config.{ Config, ConfigFactory, ConfigValueFactory } import com.typesafe.scalalogging.LazyLogging import ing.wbaa.druid.client.{ NoopRequestInterceptor, RequestInterceptor, RequestInterceptorBuilder } class BasicAuthenticationExtension(username: String, password: String) extends NoopRequestInterceptor { override def interceptRequest(request: HttpRequest): HttpRequest = request.withHeaders(Authorization(BasicHttpCredentials(username, password))) override def exportConfig: Config = ConfigFactory .empty() .withValue("username", ConfigValueFactory.fromAnyRef(username)) .withValue("password", ConfigValueFactory.fromAnyRef(password)) } object BasicAuthenticationExtension extends RequestInterceptorBuilder with LazyLogging { override def apply(config: Config): RequestInterceptor = { val username = Option(config.getString("username")).getOrElse { throw new IllegalStateException( "BasicAuthenticationExtension requires 'username' configuration parameter to be specified" ) } val password = Option(config.getString("password")).getOrElse { throw new IllegalStateException( "BasicAuthenticationExtension requires 'password' configuration parameter to be specified" ) } logger.info(s"BasicAuthenticationExtension[username=$username] created") new BasicAuthenticationExtension(username, password) } }