Skip to content

Instantly share code, notes, and snippets.

@or-shachar
Created July 2, 2018 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save or-shachar/dcfc648b5502cfd46012c7bdb90dbf11 to your computer and use it in GitHub Desktop.
Save or-shachar/dcfc648b5502cfd46012c7bdb90dbf11 to your computer and use it in GitHub Desktop.
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