Created
July 2, 2018 15:31
-
-
Save or-shachar/dcfc648b5502cfd46012c7bdb90dbf11 to your computer and use it in GitHub Desktop.
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
import java.io.FileInputStream | |
import java.security.interfaces.RSAPrivateKey | |
import java.util.Date | |
import com.auth0.jwt.JWT | |
import com.auth0.jwt.algorithms.Algorithm | |
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential | |
class GCBTokenProvider(privateKey: RSAPrivateKey, privateKeyId: String, serviceName: String, api: String, user: String) { | |
def getToken: String = { | |
val now = System.currentTimeMillis(); | |
val algorithm = Algorithm.RSA256(null,privateKey) | |
val signedJwt = JWT.create() | |
.withKeyId(privateKeyId) | |
.withIssuer(user) | |
.withSubject(user) | |
.withAudience(s"https://$serviceName/$api") | |
.withIssuedAt(new Date(now)) | |
.withExpiresAt(new Date(now + 3600 * 1000L)) | |
.sign(algorithm) | |
signedJwt | |
} | |
} | |
object GCBTokenProvider extends App { | |
val serviceName = "resultstore.googleapis.com" | |
val apiName = "google.devtools.resultstore.v2.ResultStoreFileDownload" | |
val credentials = GoogleCredential | |
.fromStream(new FileInputStream("/Users/ors/Downloads/labeldex-service-account.json")) | |
val email: String = ???// | |
val privateKey: RSAPrivateKey = credentials.getServiceAccountPrivateKey.asInstanceOf[RSAPrivateKey] | |
val privateKeyId: String = credentials.getServiceAccountPrivateKeyId | |
val provider = new GCBTokenProvider(privateKey,privateKeyId,serviceName,apiName,email) | |
println (provider.getToken) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment