-
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
dcf2912
commit f673a9e
Showing
3 changed files
with
46 additions
and
9 deletions.
There are no files selected for viewing
38 changes: 36 additions & 2 deletions
38
src/main/scala/io/otoroshi/wasm4s/scaladsl/ApikeyHelper.scala
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 |
---|---|---|
@@ -1,10 +1,44 @@ | ||
package io.otoroshi.wasm4s.scaladsl | ||
|
||
import com.auth0.jwt.algorithms.Algorithm | ||
import io.otoroshi.wasm4s.scaladsl.implicits._ | ||
import play.api.libs.json._ | ||
|
||
import java.nio.charset.StandardCharsets | ||
|
||
object ApikeyHelper { | ||
def generate(settings: WasmoSettings): String = { | ||
|
||
def generate(settings: WasmoSettings): (String, String) = { | ||
if (settings.legacyAuth) { | ||
generateJwt(settings) | ||
} else { | ||
generateBasicAuth(settings) | ||
} | ||
} | ||
|
||
def generateBasicAuth(settings: WasmoSettings): (String, String) = { | ||
val token = s"${settings.clientId}:${settings.clientSecret}".base64 | ||
s"Basic $token" | ||
("Authorization", s"Basic $token") | ||
} | ||
|
||
def generateJwt(settings: WasmoSettings): (String, String) = { | ||
val header = Json.obj( | ||
"typ" -> "JWT", | ||
"alg" -> "HS512" | ||
) | ||
val payload = Json.obj( | ||
"iss" -> "otoroshi" | ||
) | ||
("Otoroshi-User", sign(header, payload, settings.clientId)) | ||
} | ||
|
||
private def sign(headerJson: JsObject, payloadJson: JsObject, tokenSecret: String): String = { | ||
val header: String = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(Json.toBytes(headerJson)) | ||
val payload: String = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(Json.toBytes(payloadJson)) | ||
val signatureBytes: Array[Byte] = | ||
Algorithm.HMAC512(tokenSecret).sign(header.getBytes(StandardCharsets.UTF_8), payload.getBytes(StandardCharsets.UTF_8)) | ||
|
||
val signature: String = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(signatureBytes) | ||
String.format("%s.%s.%s", header, payload, signature) | ||
} | ||
} |
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