-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcae0fd
commit 8ab2b8c
Showing
5 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package daikon | ||
|
||
import daikon.RequestFlow.halt | ||
import org.eclipse.jetty.http.HttpHeader.AUTHORIZATION | ||
import org.eclipse.jetty.http.HttpHeader.WWW_AUTHENTICATE | ||
import java.nio.charset.StandardCharsets.UTF_8 | ||
import java.util.* | ||
import javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED | ||
|
||
class BasicAuthentication { | ||
|
||
private val credentials = mutableListOf<Credential>() | ||
|
||
fun addUser(username: String, password: String) { | ||
credentials.add(Credential(username, password)) | ||
} | ||
|
||
fun validate(req: Request, res: Response, realm: String) { | ||
try { | ||
val credential = credential(req.header(AUTHORIZATION.asString())) | ||
|
||
if (isForbidden(credential)) { | ||
unauthorized(res, realm) | ||
} | ||
} catch (t: Throwable) { | ||
unauthorized(res, realm) | ||
} | ||
|
||
} | ||
|
||
private fun unauthorized(res: Response, realm: String) { | ||
res.header( | ||
WWW_AUTHENTICATE.asString(), | ||
"""Basic realm="$realm", charset="UTF-8"""" | ||
) | ||
halt(res, SC_UNAUTHORIZED) | ||
} | ||
|
||
private fun isForbidden(credential: Credential): Boolean { | ||
return credentials.none { it == credential } | ||
} | ||
|
||
private fun credential(header: String): Credential { | ||
val credentials = String( | ||
Base64.getDecoder().decode(header.replace("Basic ", "", true)), | ||
UTF_8 | ||
).split(":") | ||
|
||
return Credential(credentials[0], credentials[1]) | ||
} | ||
} | ||
|
||
data class Credential(val username: String, val password: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package daikon | ||
|
||
import daikon.Localhost.get | ||
import khttp.structures.authorization.BasicAuthorization | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.eclipse.jetty.http.HttpStatus.OK_200 | ||
import org.eclipse.jetty.http.HttpStatus.UNAUTHORIZED_401 | ||
import org.junit.jupiter.api.Test | ||
|
||
class BasicAuthenticationHttpTest { | ||
|
||
@Test | ||
fun routing() { | ||
HttpServer() | ||
.get("/") { _, res -> res.status(OK_200)} | ||
.basicAuth("/foo", "realm") | ||
.basicAuth("/bar*") | ||
.basicAuth("/baz/:name") | ||
.start().use { | ||
assertThat(get("/").statusCode).isEqualTo(OK_200) | ||
assertThat(get("/foo").statusCode).isEqualTo(UNAUTHORIZED_401) | ||
assertThat(get("/bar/baz").statusCode).isEqualTo(UNAUTHORIZED_401) | ||
assertThat(get("/baz/alex").statusCode).isEqualTo(UNAUTHORIZED_401) | ||
} | ||
} | ||
|
||
@Test | ||
fun `authenticate user`() { | ||
HttpServer() | ||
.basicAuthUser("Marco", "secret") | ||
.basicAuth("/") | ||
.get("/") { _, res -> res.status(OK_200)} | ||
.start().use { | ||
assertThat(get("/").statusCode).isEqualTo(UNAUTHORIZED_401) | ||
assertThat(get("/", auth = BasicAuthorization("Marco", "secret")).statusCode).isEqualTo(OK_200) | ||
} | ||
} | ||
|
||
@Test | ||
fun `wrong credential`() { | ||
HttpServer() | ||
.basicAuthUser("Marco", "secret") | ||
.basicAuth("/") | ||
.get("/") { _, res -> res.status(OK_200)} | ||
.start().use { | ||
assertThat(get("/", auth = BasicAuthorization("Marco", "wrong")).statusCode).isEqualTo(UNAUTHORIZED_401) | ||
assertThat(get("/", auth = BasicAuthorization("wrong", "secret")).statusCode).isEqualTo(UNAUTHORIZED_401) | ||
} | ||
} | ||
|
||
@Test | ||
fun `supports utf-8`() { | ||
HttpServer() | ||
.basicAuthUser("ìù", "èéàò") | ||
.basicAuth("/") | ||
.get("/") { _, res -> res.status(OK_200)} | ||
.start().use { | ||
assertThat(get("/", auth = BasicAuthorization("ìù", "èéàò")).statusCode).isEqualTo(OK_200) | ||
} | ||
} | ||
} |