115 lines
3.8 KiB
Kotlin
115 lines
3.8 KiB
Kotlin
@file:Suppress("unused", "unused")
|
|
|
|
package com.example.loginapi
|
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore
|
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
|
|
import com.fasterxml.jackson.annotation.JsonProperty
|
|
import org.springframework.http.*
|
|
import org.springframework.util.LinkedMultiValueMap
|
|
import org.springframework.util.MultiValueMap
|
|
import org.springframework.web.client.RestTemplate
|
|
import java.io.Serializable
|
|
import java.nio.charset.StandardCharsets
|
|
import java.util.*
|
|
|
|
/**
|
|
* @author sombochea
|
|
* @since 1.0
|
|
*/
|
|
object RestClientUtils {
|
|
private const val BEAN_NAME = "restTemplate"
|
|
|
|
private var restTemplate: RestTemplate? = null
|
|
|
|
@JvmStatic
|
|
fun setRestTemplate(restTemplate: RestTemplate?) {
|
|
RestClientUtils.restTemplate = restTemplate
|
|
}
|
|
|
|
@JvmStatic
|
|
fun getRestTemplate(): RestTemplate {
|
|
if (restTemplate == null) {
|
|
restTemplate = RestTemplate()
|
|
}
|
|
return restTemplate ?: throw Exception("rest client service load failed")
|
|
}
|
|
|
|
fun login(username: String, password: String): OAuthToken? {
|
|
val authEndpoint = "https://preprod-api-auth.staging.cubetiqs.com/api/oauth/token"
|
|
val httpHeaders = getHttpHeadersConfig()
|
|
val body: MultiValueMap<String, String> = LinkedMultiValueMap()
|
|
body.add("grant_type", "password")
|
|
body.add("username", username)
|
|
body.add("password", password)
|
|
|
|
val httpEntity = HttpEntity(body, httpHeaders)
|
|
|
|
println(httpEntity)
|
|
|
|
return getRestTemplate().postForEntity(authEndpoint, httpEntity, OAuthToken::class.java).body
|
|
}
|
|
|
|
private fun getHttpHeadersConfig(): HttpHeaders {
|
|
val httpHeaders = HttpHeaders()
|
|
val client = "cubetiq-clinic-dev"
|
|
val secret = "123456"
|
|
httpHeaders.contentType = MediaType.APPLICATION_FORM_URLENCODED
|
|
val clientDetail = "$client:$secret"
|
|
val oauthCodes = Base64.getEncoder().encode(clientDetail.toByteArray(StandardCharsets.US_ASCII))
|
|
httpHeaders["Authorization"] = "Basic " + String(oauthCodes)
|
|
httpHeaders["Tenant-ID"] = "TNA-00013067"
|
|
httpHeaders["User-Type"] = "INTERNAL"
|
|
return httpHeaders
|
|
}
|
|
|
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
data class OAuthToken(
|
|
@JsonProperty(value = "access_token")
|
|
var accessToken: String? = null,
|
|
@JsonProperty(value = "token_type")
|
|
var tokenType: String? = null,
|
|
@JsonProperty(value = "refresh_token")
|
|
var refreshToken: String? = null,
|
|
@JsonProperty(value = "expires_in")
|
|
var expiresIn: Long? = null,
|
|
@JsonProperty(value = "scope")
|
|
var scope: String? = null,
|
|
@JsonProperty(value = "auditor")
|
|
var auditor: String? = null,
|
|
@JsonProperty(value = "tenant")
|
|
var tenant: String? = null,
|
|
@JsonProperty(value = "user_id")
|
|
var userId: String? = null,
|
|
@JsonProperty(value = "username")
|
|
var username: String? = null,
|
|
@JsonProperty(value = "jti")
|
|
var jti: String? = null,
|
|
|
|
var passcode: Boolean? = null,
|
|
var configs: Map<String, Any?>? = null,
|
|
|
|
@JsonProperty(value = "current_branch_id")
|
|
var currentBranchId: String? = null,
|
|
@JsonProperty(value = "current_branch")
|
|
var currentBranch: String? = null,
|
|
) : Serializable {
|
|
@JsonIgnore
|
|
fun addConfig(key: String, value: Any?) = apply {
|
|
if (this.configs == null) {
|
|
this.configs = mutableMapOf()
|
|
}
|
|
(this.configs as MutableMap)[key] = value
|
|
}
|
|
|
|
@JsonIgnore
|
|
fun addConfigs(configs: Map<String, Any?>?) = apply {
|
|
if (this.configs == null) {
|
|
this.configs = mutableMapOf()
|
|
}
|
|
if (configs != null) {
|
|
(this.configs as MutableMap).putAll(configs)
|
|
}
|
|
}
|
|
}
|
|
} |