-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RFR-471] Add api/v4 with OAuth2 (#158)
* Fix docker configuration * WIP: upload works * [RFR-520] Get user id for upload * Remove login endpoint from openapi yml * [RFR-526] Add API v4 and move deprecated classes to v6 package * Add openapi v3 * Replace v3 mentions with v4 * [RFR-538] Inject keycloak parameters into config * [RFR-543] Mock Keycloak server in tests * Cleanup * Fix openapi documenation * Cleanup tests for invalid credentials * Substitute keycloak for oauth * Fix build * [RFR-266] Fix CI
- Loading branch information
Showing
55 changed files
with
1,350 additions
and
462 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/de/cyface/collector/auth/AuthHandlerBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2023 Cyface GmbH | ||
* | ||
* This file is part of the Cyface Data Collector. | ||
* | ||
* The Cyface Data Collector is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Cyface Data Collector is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with the Cyface Data Collector. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package de.cyface.collector.auth | ||
|
||
import io.vertx.core.Future | ||
import io.vertx.ext.web.handler.OAuth2AuthHandler | ||
|
||
/** | ||
* Interface for the builder which creates an [OAuth2AuthHandler] to allow mocking. | ||
* | ||
* @author Armin Schnabel | ||
* @version 1.0.0 | ||
* @since 7.0.0 | ||
*/ | ||
interface AuthHandlerBuilder { | ||
|
||
/** | ||
* Start the creation process of a [AuthHandlerBuilder] and provide a [Future], that will be notified about | ||
* successful or failed completion. | ||
*/ | ||
fun create(): Future<OAuth2AuthHandler> | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/kotlin/de/cyface/collector/auth/MockedHandlerBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2023 Cyface GmbH | ||
* | ||
* This file is part of the Cyface Data Collector. | ||
* | ||
* The Cyface Data Collector is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Cyface Data Collector is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with the Cyface Data Collector. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package de.cyface.collector.auth | ||
|
||
import io.vertx.core.Future | ||
import io.vertx.core.json.JsonObject | ||
import io.vertx.ext.auth.impl.UserImpl | ||
import io.vertx.ext.web.Route | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.ext.web.handler.OAuth2AuthHandler | ||
import java.util.UUID | ||
|
||
/** | ||
* Mocked OAuth2 builder which creates an OAuth2 handler for testing. | ||
* | ||
* @author Armin Schnabel | ||
* @version 1.0.0 | ||
* @since 7.0.0 | ||
*/ | ||
class MockedHandlerBuilder : AuthHandlerBuilder { | ||
|
||
override fun create(): Future<OAuth2AuthHandler> { | ||
val handler: OAuth2AuthHandler = object : OAuth2AuthHandler { | ||
override fun handle(event: RoutingContext) { | ||
val principal = JsonObject() | ||
.put("username", "test-user") | ||
.put("sub", UUID.randomUUID()) // user id | ||
val user = UserImpl(principal, JsonObject()) | ||
event.setUser(user) | ||
|
||
// From AuthenticationHandlerImpl.handle @ `authenticate(ctx, authN -> {..})` | ||
// event.session()?.regenerateId() - this leads to SessionExpired exception, thus, commented out | ||
// proceed with the router | ||
if (!event.request().isEnded) { | ||
event.request().resume() | ||
} | ||
postAuthentication(event) | ||
} | ||
|
||
// From AuthenticationHandlerInternal | ||
private fun postAuthentication(event: RoutingContext) { | ||
event.next() | ||
} | ||
|
||
override fun extraParams(extraParams: JsonObject?): OAuth2AuthHandler { | ||
return this | ||
} | ||
|
||
override fun withScope(scope: String?): OAuth2AuthHandler { | ||
return this | ||
} | ||
|
||
override fun withScopes(scopes: MutableList<String>?): OAuth2AuthHandler { | ||
return this | ||
} | ||
|
||
override fun prompt(prompt: String?): OAuth2AuthHandler { | ||
return this | ||
} | ||
|
||
override fun pkceVerifierLength(length: Int): OAuth2AuthHandler { | ||
return this | ||
} | ||
|
||
override fun setupCallback(route: Route?): OAuth2AuthHandler { | ||
return this | ||
} | ||
} | ||
return Future.succeededFuture(handler) | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/de/cyface/collector/auth/OAuth2HandlerBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2023 Cyface GmbH | ||
* | ||
* This file is part of the Cyface Data Collector. | ||
* | ||
* The Cyface Data Collector is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Cyface Data Collector is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with the Cyface Data Collector. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package de.cyface.collector.auth | ||
|
||
import io.vertx.core.Future | ||
import io.vertx.core.Promise | ||
import io.vertx.core.Vertx | ||
import io.vertx.ext.auth.oauth2.OAuth2Options | ||
import io.vertx.ext.auth.oauth2.providers.KeycloakAuth | ||
import io.vertx.ext.web.Router | ||
import io.vertx.ext.web.handler.OAuth2AuthHandler | ||
import java.net.URL | ||
|
||
/** | ||
* Keycloak OAuth2 builder which creates an OAuth2 handler. | ||
* | ||
* @author Armin Schnabel | ||
* @version 1.0.0 | ||
* @since 7.0.0 | ||
* @property vertx | ||
* @property apiRouter | ||
* @property callbackUrl The callback URL you entered in your provider admin console. | ||
* @property options the oauth configuration. | ||
*/ | ||
class OAuth2HandlerBuilder( | ||
private val vertx: Vertx, | ||
private val apiRouter: Router, | ||
private val callbackUrl: URL, | ||
private val options: OAuth2Options, | ||
) : AuthHandlerBuilder { | ||
|
||
override fun create(): Future<OAuth2AuthHandler> { | ||
val promise = Promise.promise<OAuth2AuthHandler>() | ||
|
||
KeycloakAuth.discover(vertx, options) | ||
.onSuccess { | ||
val callbackAddress = apiRouter.get(callbackUrl.path) | ||
val oauth2Handler = OAuth2AuthHandler.create(vertx, it, callbackUrl.toURI().toString()) | ||
.setupCallback(callbackAddress) | ||
promise.complete(oauth2Handler) | ||
} | ||
.onFailure { promise.fail(it) } | ||
|
||
return promise.future() | ||
} | ||
} |
Oops, something went wrong.