All Downloads are FREE. Search and download functionalities are using the official Maven repository.

euszjaje.jira4s-core_3.2.4.0.source-code.JiraConfig.scala Maven / Gradle / Ivy

There is a newer version: 2.4.1
Show newest version
package io.gitlab.mateuszjaje.jiraclient

import com.typesafe.config.Config

import java.util.Base64

trait AuthMechanism extends Product with Serializable {
  def authHeader: String
}
case class Basic(login: String, password: String) extends AuthMechanism {
  assert(login.nonEmpty && password.nonEmpty, "Jira credentials empty!")
  override val authHeader = s"Basic ${Base64.getEncoder.encodeToString(s"$login:$password".getBytes("utf-8"))}"
}
case class AccessToken(token: String) extends AuthMechanism {
  override val authHeader = s"Bearer $token"
}

case class JiraConfig(address: String, auth: AuthMechanism) {
  val getAuthHeaderValue = auth.authHeader
}

object JiraConfig {
  def fromConfig(config: Config) = {
    val authMechanism = if (config.hasPath("auth")) {
      val authConfig = config.getConfig("auth")
      authConfig.getString("type") match {
        case "basic"        => Basic(authConfig.getString("login"), authConfig.getString("password"))
        case "access-token" => AccessToken(authConfig.getString("access-token"))
      }
    } else {
      Basic(config.getString("login"), config.getString("password"))
    }
    new JiraConfig(
      config.getString("address"),
      authMechanism,
    )

  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy