Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/b1057c9c201b29f5399384f40ce85520 to your computer and use it in GitHub Desktop.
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/5ad556fcb178c52d4eb278a338c988e7956604b5
// 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.2.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