Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide way to set authentication #1617

Open
domdorn opened this issue May 13, 2022 · 0 comments
Open

Provide way to set authentication #1617

domdorn opened this issue May 13, 2022 · 0 comments
Labels
client enhancement New feature or request help wanted Issues that the core team will likely not have time to work on

Comments

@domdorn
Copy link

domdorn commented May 13, 2022

Please include the following file in the sourcecode to allow users to set credentials:

import akka.http.scaladsl.model.headers.Authorization
import io.grpc.{CallCredentials, Metadata, Status}

import java.util.concurrent.Executor
import scala.util.{Failure, Success, Try}

object AuthorizationCallCredentials {
  import io.grpc.Metadata.ASCII_STRING_MARSHALLER
  val AuthKey: Metadata.Key[String] = Metadata.Key.of("Authorization", ASCII_STRING_MARSHALLER);

}

final case class AuthorizationCallCredentials(auth: Authorization) extends CallCredentials {
  override def applyRequestMetadata(
    requestInfo: CallCredentials.RequestInfo,
    appExecutor: Executor,
    metadataApplier: CallCredentials.MetadataApplier): Unit =
    // implemented similar to official example from:
    // https://github.com/grpc/grpc-java/blob/master/examples/example-jwt-auth/src/main/java/io/grpc/examples/jwtauth/JwtCredential.java
    appExecutor.execute { () =>
      val headers = new Metadata()
      headers.put(AuthorizationCallCredentials.AuthKey, auth.credentials.scheme() + " " + auth.credentials.token())
      Try(metadataApplier.apply(headers)) match {
        case Failure(ex) => metadataApplier.fail(Status.UNAUTHENTICATED.withCause(ex))
        case Success(_)  => ()
      }
    }

  override def thisUsesUnstableApi(): Unit = ()
}

This allows to set any credential type that is already supported by Akka-HTTP, e.g. like

  private val cred = Authorization(
    BasicHttpCredentials(
      config.getString("clients.timeseries.username"),
      config.getString("clients.timeseries.password")
    ))

  val grpcClientSettings = GrpcClientSettings.connectToServiceAt(host, port)(akka).withCallCredentials(AuthorizationCallCredentials(cred)))

Mentioning it, e.g. somewhere close to https://doc.akka.io/docs/akka-grpc/current/server/akka-http.html#example-authentication-authorization would probably also be helpful.

@raboof raboof added enhancement New feature or request client help wanted Issues that the core team will likely not have time to work on labels May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client enhancement New feature or request help wanted Issues that the core team will likely not have time to work on
Projects
None yet
Development

No branches or pull requests

2 participants