Last active
May 25, 2024 10:19
-
-
Save dacr/b1057c9c201b29f5399384f40ce85520 to your computer and use it in GitHub Desktop.
small scala function to generate a basic authentication Authentication token / published by https://github.com/dacr/code-examples-manager #6d5cecb8-9159-40a5-9214-7bb752455e99/731c0bac4f46b1a162a341c2419f69cc3f42a107
This file contains hidden or 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
// summary : small scala function to generate a basic authentication Authentication token | |
// keywords : scala, token, credential, basic-auth, authentication, base64, encode, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 6d5cecb8-9159-40a5-9214-7bb752455e99 | |
// created-on : 2020-11-10T06:22:34Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
// --------------------- | |
def basicAuthToken(username: String, password: String, charsetName:String="UTF-8"): String = { | |
val tokenBytes = s"$username:$password".getBytes(charsetName) | |
val tokenB64 = java.util.Base64.getEncoder.encodeToString(tokenBytes) | |
s"Basic $tokenB64" | |
} | |
val token = basicAuthToken("root","root") | |
assert(token == "Basic cm9vdDpyb290") | |
println(s"Authorization: $token") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment