Hi All,
Am new to scala, am using scala only for gatling load testing. I want to generate jwt token using scala. I have tried my best but the code is not working.
Please post me the code or example with respect to below code
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.client.json.JsonFactory
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.json.webtoken.JsonWebSignature
import com.google.api.client.json.webtoken.JsonWebToken
import com.google.api.client.util.Clock
import com.google.gson.Gson
import lombok.Data
import org.apache.commons.io.Charsets
import org.apache.commons.io.IOUtils
import org.apache.http.HttpResponse
import org.apache.http.NameValuePair
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.HttpPost
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.message.BasicNameValuePair
import java.io.ByteArrayInputStream
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream
import java.net.URISyntaxException
import java.security.GeneralSecurityException
import java.util.ArrayList
import java.util.Base64
import java.util.List
import JWTClient._
import play.api.libs.json.JsObject
//remove if not needed
import scala.collection.JavaConversions._
object JWTClient {
private var inputStream: InputStream = _
def getJwt(issuer: String, audience: String): String = {
val serviceAccountFile: String =
"some-thing"
val decoder: Base64.Decoder = Base64.getDecoder
val stream: InputStream = new ByteArrayInputStream(
decoder.decode(serviceAccountFile))
val credential: GoogleCredential = GoogleCredential.fromStream(stream)
val privateKeyId: String = credential.getServiceAccountPrivateKeyId
val header: JsonWebSignature.Header = new JsonWebSignature.Header()
header.setAlgorithm("RS256")
header.setType("JWT")
header.setKeyId(privateKeyId)
val clock: Clock = Clock.SYSTEM
val currentTime: Long = clock.currentTimeMillis()
val payload: JsonWebSignature.Payload = new JsonWebToken.Payload()
payload.setIssuedAtTimeSeconds(currentTime / 1000)
payload.setExpirationTimeSeconds(currentTime / 1000 + 3600)
payload.setIssuer(issuer)
payload.set("target_audience", audience)
payload.setAudience("https://www.googleapis.com/oauth2/v4/token")
val jsonFactory: JsonFactory = JacksonFactory.getDefaultInstance
var signedJwt: String = null
try signedJwt = JsonWebSignature.signUsingRsaSha256(
credential.getServiceAccountPrivateKey,
jsonFactory,
header,
payload)
catch {
case e: GeneralSecurityException => e.printStackTrace()
}
signedJwt
}
def getAccessToken(issuer: String, audience: String): String = {
val httpClient: DefaultHttpClient = new DefaultHttpClient()
val postRequest: HttpPost = new HttpPost(
"https://www.googleapis.com/oauth2/v4/token")
postRequest.addHeader("Content-Type", "application/x-www-form-urlencoded")
val parameters: List[NameValuePair] = new ArrayList[NameValuePair](2)
parameters.add(
new BasicNameValuePair("grant_type",
"urn:ietf:params:oauth:grant-type:jwt-bearer"))
parameters.add(
new BasicNameValuePair("assertion", getJwt(issuer, audience)))
postRequest.setEntity(new UrlEncodedFormEntity(parameters))
val response: HttpResponse = httpClient.execute(postRequest)
val responseContent: String = IOUtils.toString(
response.getEntity.getContent,
Charsets.toCharset("UTF-8"))
val responseToken: ResponseToken =
getResult(responseContent, classOf[ResponseToken])
println(responseToken.getId_token)
responseToken.getId_token
}
def getResult[T](json: String, classOfT: Class[T]): T = {
val gson: Gson = new Gson()
gson.fromJson(json, classOfT)
}
def main(args: Array[String]): Unit = {
val ISSUER: String =
"Some-thing"
val AUDIENCE: String =
"Some-thing"
System.out.print(getAccessToken(ISSUER, AUDIENCE))
}
}
class JWTClient {
@Data
class ResponseToken {
var id_token: String = _
}
}
please do the needful