diff options
author | adamw <adam@warski.org> | 2017-07-15 18:40:50 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-15 18:40:50 +0200 |
commit | b053c91551b924100fa8261e4f405bc40ce53147 (patch) | |
tree | fc79e2f9152fa0f9e4e1d4d50d4a00e69445fe3d /core/src/main/scala/com | |
parent | a3b03abe52b383488a3d8349d5b851d80050c4f2 (diff) | |
download | sttp-b053c91551b924100fa8261e4f405bc40ce53147.tar.gz sttp-b053c91551b924100fa8261e4f405bc40ce53147.tar.bz2 sttp-b053c91551b924100fa8261e4f405bc40ce53147.zip |
Basic auth
Diffstat (limited to 'core/src/main/scala/com')
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/model/package.scala | 2 | ||||
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/package.scala | 19 |
2 files changed, 20 insertions, 1 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/model/package.scala b/core/src/main/scala/com/softwaremill/sttp/model/package.scala index 982bc7c..3504866 100644 --- a/core/src/main/scala/com/softwaremill/sttp/model/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/model/package.scala @@ -1,6 +1,6 @@ package com.softwaremill.sttp -import java.io.{File, InputStream} +import java.io.InputStream import java.nio.ByteBuffer import java.nio.file.Path diff --git a/core/src/main/scala/com/softwaremill/sttp/package.scala b/core/src/main/scala/com/softwaremill/sttp/package.scala index fdf537d..9061f5f 100644 --- a/core/src/main/scala/com/softwaremill/sttp/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/package.scala @@ -4,6 +4,7 @@ import java.io.{File, InputStream} import java.net.URI import java.nio.ByteBuffer import java.nio.file.Path +import java.util.Base64 import com.softwaremill.sttp.model._ @@ -145,6 +146,10 @@ package object sttp { cookies(cs.map(c => (c.name, c.value)): _*) def cookies(nvs: (String, String)*): RequestTemplate[U] = header(CookieHeader, nvs.map(p => p._1 + "=" + p._2).mkString("; ")) + def auth: SpecifyAuthScheme[U] = + new SpecifyAuthScheme[U](AuthorizationHeader, this) + def proxyAuth: SpecifyAuthScheme[U] = + new SpecifyAuthScheme[U](ProxyAuthorizationHeader, this) /** * If content type is not yet specified, will be set to `text/plain` with `utf-8` encoding. @@ -224,6 +229,18 @@ package object sttp { } } + class SpecifyAuthScheme[U[_]](hn: String, rt: RequestTemplate[U]) { + def basic(user: String, password: String): RequestTemplate[U] = { + val c = new String( + Base64.getEncoder.encode(s"$user:$password".getBytes(Utf8)), + Utf8) + rt.header(hn, s"Basic $c") + } + + def bearer(token: String): RequestTemplate[U] = + rt.header(hn, s"Bearer $token") + } + object RequestTemplate { val empty: RequestTemplate[Empty] = RequestTemplate[Empty](None, None, NoBody, Vector()) @@ -242,6 +259,8 @@ package object sttp { private[sttp] val ContentLengthHeader = "Content-Length" private[sttp] val SetCookieHeader = "Set-Cookie" private[sttp] val CookieHeader = "Cookie" + private[sttp] val AuthorizationHeader = "Authorization" + private[sttp] val ProxyAuthorizationHeader = "Proxy-Authorization" private val Utf8 = "utf-8" private val ApplicationOctetStreamContentType = "application/octet-stream" |