forked from navikt/mock-oauth2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAuth2HttpResponse.kt
177 lines (161 loc) · 6.25 KB
/
OAuth2HttpResponse.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package no.nav.security.mock.oauth2.http
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.nimbusds.oauth2.sdk.ErrorObject
import com.nimbusds.oauth2.sdk.ResponseMode
import com.nimbusds.openid.connect.sdk.AuthenticationSuccessResponse
import io.netty.handler.codec.http.HttpHeaderNames
import no.nav.security.mock.oauth2.templates.TemplateMapper
import no.nav.security.mock.oauth2.token.KeyGenerator
import okhttp3.Headers
val objectMapper: ObjectMapper = jacksonObjectMapper()
val templateMapper: TemplateMapper = TemplateMapper.create {}
data class OAuth2HttpResponse(
val headers: Headers = Headers.headersOf(),
val status: Int,
val body: String? = null,
val bytesBody: ByteArray? = null,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as OAuth2HttpResponse
if (headers != other.headers) return false
if (status != other.status) return false
if (body != other.body) return false
if (bytesBody != null) {
if (other.bytesBody == null) return false
if (!bytesBody.contentEquals(other.bytesBody)) return false
} else if (other.bytesBody != null) {
return false
}
return true
}
override fun hashCode(): Int {
var result = headers.hashCode()
result = 31 * result + status
result = 31 * result + (body?.hashCode() ?: 0)
result = 31 * result + (bytesBody?.contentHashCode() ?: 0)
return result
}
}
data class WellKnown(
val issuer: String,
@JsonProperty("authorization_endpoint")
val authorizationEndpoint: String,
@JsonProperty("end_session_endpoint")
val endSessionEndpoint: String,
@JsonProperty("revocation_endpoint")
val revocationEndpoint: String,
@JsonProperty("token_endpoint")
val tokenEndpoint: String,
@JsonProperty("userinfo_endpoint")
val userInfoEndpoint: String,
@JsonProperty("jwks_uri")
val jwksUri: String,
@JsonProperty("introspection_endpoint")
val introspectionEndpoint: String,
@JsonProperty("response_types_supported")
val responseTypesSupported: List<String> = listOf("code", "none", "id_token", "token"),
@JsonProperty("response_modes_supported")
val responseModesSupported: List<String> = listOf("query", "fragment", "form_post"),
@JsonProperty("subject_types_supported")
val subjectTypesSupported: List<String> = listOf("public"),
@JsonProperty("id_token_signing_alg_values_supported")
val idTokenSigningAlgValuesSupported: List<String> = (KeyGenerator.ecAlgorithmFamily + KeyGenerator.rsaAlgorithmFamily).map { it.name }.toList(),
@JsonProperty("code_challenge_methods_supported")
val codeChallengeMethodsSupported: List<String> = listOf("plain", "S256"),
)
@JsonInclude(JsonInclude.Include.NON_NULL)
data class OAuth2TokenResponse(
@JsonProperty("token_type")
val tokenType: String,
@JsonProperty("issued_token_type")
val issuedTokenType: String? = null,
@JsonProperty("id_token")
val idToken: String? = null,
@JsonProperty("access_token")
val accessToken: String?,
@JsonProperty("refresh_token")
val refreshToken: String? = null,
@JsonProperty("expires_in")
val expiresIn: Int = 0,
@JsonProperty("scope")
val scope: String? = null,
)
fun json(anyObject: Any): OAuth2HttpResponse =
OAuth2HttpResponse(
headers =
Headers.headersOf(
HttpHeaderNames.CONTENT_TYPE.toString(),
"application/json;charset=UTF-8",
),
status = 200,
body =
when (anyObject) {
is String -> anyObject
else ->
objectMapper
.enable(SerializationFeature.INDENT_OUTPUT)
.writeValueAsString(anyObject)
},
)
fun html(content: String): OAuth2HttpResponse =
OAuth2HttpResponse(
headers =
Headers.headersOf(
HttpHeaderNames.CONTENT_TYPE.toString(),
"text/html;charset=UTF-8",
),
status = 200,
body = content,
)
fun redirect(
location: String,
headers: Headers = Headers.headersOf(),
): OAuth2HttpResponse =
OAuth2HttpResponse(
headers = Headers.headersOf(HttpHeaderNames.LOCATION.toString(), location).newBuilder().addAll(headers).build(),
status = 302,
)
fun notFound(body: String? = null): OAuth2HttpResponse = OAuth2HttpResponse(status = 404, body = body)
fun methodNotAllowed(): OAuth2HttpResponse = OAuth2HttpResponse(status = 405, body = "method not allowed")
fun authenticationSuccess(authenticationSuccessResponse: AuthenticationSuccessResponse): OAuth2HttpResponse {
return when (authenticationSuccessResponse.responseMode) {
ResponseMode.FORM_POST -> {
OAuth2HttpResponse(
status = 200,
body =
templateMapper.authorizationCodeResponseHtml(
authenticationSuccessResponse.redirectionURI.toString(),
authenticationSuccessResponse.authorizationCode.value,
authenticationSuccessResponse.state.value,
),
)
}
else ->
OAuth2HttpResponse(
headers = Headers.headersOf(HttpHeaderNames.LOCATION.toString(), authenticationSuccessResponse.toURI().toString()),
status = 302,
)
}
}
fun oauth2Error(error: ErrorObject): OAuth2HttpResponse {
val responseCode = error.httpStatusCode.takeUnless { it == 302 } ?: 400
return OAuth2HttpResponse(
headers =
Headers.headersOf(
HttpHeaderNames.CONTENT_TYPE.toString(),
"application/json;charset=UTF-8",
),
status = responseCode,
body =
objectMapper
.enable(SerializationFeature.INDENT_OUTPUT)
.writeValueAsString(error.toJSONObject())
.lowercase(),
)
}