1
1
package no.nav.dagpenger.texas
2
2
3
+ import com.fasterxml.jackson.annotation.JsonValue
3
4
import io.ktor.client.HttpClient
4
5
import io.ktor.client.call.body
5
6
import io.ktor.client.request.header
6
7
import io.ktor.client.request.post
7
8
import io.ktor.client.request.setBody
8
9
import io.ktor.http.HttpStatusCode
9
10
11
+ data class IntrospectRequest (
12
+ val identity_provider : IdentityProvider ,
13
+ val token : String ,
14
+ )
15
+
16
+ sealed class IntrospectResponse (val active : Boolean ) {
17
+ data class Valid (val claims : Map <String , Any >) : IntrospectResponse(active = true )
18
+
19
+ data class Invalid (val error : String ) : IntrospectResponse(active = false )
20
+ }
21
+
22
+ enum class IdentityProvider (
23
+ @JsonValue
24
+ val value : String ,
25
+ ) {
26
+ ENTRA_ID (" azuread" ),
27
+ }
28
+
10
29
data class TokenRequest (
11
- val identity_provider : String ,
30
+ val identity_provider : IdentityProvider ,
12
31
val target : String ,
13
32
val resource : String? = null ,
14
33
val skip_cache : Boolean = false ,
15
34
)
16
35
17
36
data class TokenExchangeRequest (
18
- val identity_provider : String ,
37
+ val identity_provider : IdentityProvider ,
19
38
val target : String ,
20
39
val user_token : String ,
21
40
val skip_cache : Boolean = false ,
@@ -43,55 +62,15 @@ class BadRequestException(httpStatusCode: HttpStatusCode, errorResponse: ErrorRe
43
62
class ServerError (httpStatusCode : HttpStatusCode , errorResponse : ErrorResponse ) :
44
63
RequestError (httpStatusCode, errorResponse)
45
64
46
- class EntraKlient (
47
- tokenEndpoint : String ,
48
- tokenExchangeEndpoint : String ,
49
- httpClient : HttpClient = defaultHttpClient(),
50
- ) {
51
- companion object {
52
- const val IDENTITY_PROVIDER = " azuread"
53
- }
54
-
55
- private val texasKlient: TexasKlient =
56
- TexasKlient (
57
- tokenEndpoint = tokenEndpoint,
58
- tokenExchangeEndpoint = tokenExchangeEndpoint,
59
- httpClient = httpClient,
60
- )
61
-
62
- suspend fun accessToken (
63
- target : String ,
64
- resource : String? = null,
65
- skipCache : Boolean = true,
66
- ): TokenResponse =
67
- texasKlient.accessToken(
68
- target = target,
69
- identityProvider = IDENTITY_PROVIDER ,
70
- resource = resource,
71
- skipCache = skipCache,
72
- )
73
-
74
- suspend fun exchangeToken (
75
- target : String ,
76
- token : String ,
77
- skipCache : Boolean = false,
78
- ): TokenResponse =
79
- texasKlient.exchangeToken(
80
- target = target,
81
- token = token,
82
- identityProvider = IDENTITY_PROVIDER ,
83
- skipCache = skipCache,
84
- )
85
- }
86
-
87
65
class TexasKlient (
88
66
private val tokenEndpoint : String ,
89
67
private val tokenExchangeEndpoint : String ,
68
+ private val introspectEndpoint : String ,
90
69
private val httpClient : HttpClient = defaultHttpClient(),
91
70
) {
92
71
suspend fun accessToken (
93
72
target : String ,
94
- identityProvider : String ,
73
+ identityProvider : IdentityProvider ,
95
74
resource : String? = null,
96
75
skipCache : Boolean ,
97
76
): TokenResponse {
@@ -107,19 +86,40 @@ class TexasKlient(
107
86
suspend fun exchangeToken (
108
87
target : String ,
109
88
token : String ,
110
- identityProvider : String ,
89
+ identityProvider : IdentityProvider ,
111
90
skipCache : Boolean ,
112
91
): TokenResponse {
113
- return httpClient.post(tokenExchangeEndpoint) {
114
- header(" Content-Type" , " application/json" )
115
- setBody(
116
- TokenExchangeRequest (
117
- identity_provider = identityProvider,
118
- target = target,
119
- user_token = token,
120
- skip_cache = skipCache,
121
- ),
122
- )
123
- }.body<TokenResponse >()
92
+ return kotlin.runCatching {
93
+ httpClient.post(tokenExchangeEndpoint) {
94
+ header(" Content-Type" , " application/json" )
95
+ setBody(
96
+ TokenExchangeRequest (
97
+ identity_provider = identityProvider,
98
+ target = target,
99
+ user_token = token,
100
+ skip_cache = skipCache,
101
+ ),
102
+ )
103
+ }.body<TokenResponse >()
104
+ }.onFailure {
105
+ }.getOrThrow()
106
+ }
107
+
108
+ suspend fun introspect (
109
+ identityProvider : IdentityProvider ,
110
+ token : String ,
111
+ ): IntrospectResponse {
112
+ return kotlin.runCatching {
113
+ httpClient.post(introspectEndpoint) {
114
+ header(" Content-Type" , " application/json" )
115
+ setBody(
116
+ IntrospectRequest (
117
+ identity_provider = identityProvider,
118
+ token = token,
119
+ ),
120
+ )
121
+ }.body<IntrospectResponse >()
122
+ }.onFailure {
123
+ }.getOrThrow()
124
124
}
125
125
}
0 commit comments