Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
[RFR-510] Support OAuth2 (#10)
Browse files Browse the repository at this point in the history
* [RFR-520] Move pre-oauth classes to v2 package

* [RFR-520] Add User model with UUID from OAuth

* Documentation & cleanup

* Replace hamcrest with Junit.Assertion
  • Loading branch information
hb0 authored Jun 9, 2023
1 parent 6dd7597 commit fb4ba1f
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 74 deletions.
1 change: 0 additions & 1 deletion api-test-environment/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ dependencies {
// Test Dependencies
implementation "org.junit.jupiter:junit-jupiter:$junitVersion"
implementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
implementation "org.hamcrest:hamcrest:$hamcrestVersion"
implementation "org.mockito:mockito-core:$mockitoVersion"
implementation "io.vertx:vertx-junit5:$vertxVersion"
implementation "io.vertx:vertx-web-client:$vertxVersion"
Expand Down
1 change: 0 additions & 1 deletion api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ dependencies {
testImplementation project(':api-test-environment')
testImplementation "io.vertx:vertx-junit5:$vertxVersion"
testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
implementation "org.hamcrest:hamcrest:$hamcrestVersion"
}
9 changes: 4 additions & 5 deletions api/src/main/java/de/cyface/api/PauseAndResumeStrategy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Cyface GmbH
* Copyright 2022-2023 Cyface GmbH
*
* This file is part of the Cyface API Library.
*
Expand All @@ -19,17 +19,16 @@
package de.cyface.api;

import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.RoutingContext;

/**
* The interface for pause and resume strategies to be used which wraps async calls in
* {@link Authorizer#handle(RoutingContext)}.
* The interface for pause and resume strategies to be used which wraps async calls, e.g. in
* {@code AuthorizationHandler}s in APIs which use this library (e.g. collector, incentives).
* <p>
* Use {@link PauseAndResumeBeforeBodyParsing} when the `BodyHandler` is not executed before that handler [DAT-749] or
* {@link PauseAndResumeAfterBodyParsing} otherwise.
*
* @author Armin Schnabel
* @version 1.0.1
* @version 1.0.2
* @since 1.0.0
*/
public interface PauseAndResumeStrategy {
Expand Down
23 changes: 12 additions & 11 deletions api/src/main/java/de/cyface/api/model/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Cyface GmbH
* Copyright 2022-2023 Cyface GmbH
*
* This file is part of the Cyface API Library.
*
Expand All @@ -18,26 +18,27 @@
*/
package de.cyface.api.model;

import io.vertx.core.json.JsonObject;
import org.bson.types.ObjectId;
import static io.vertx.ext.auth.mongo.MongoAuthorization.DEFAULT_USERNAME_FIELD;

import java.util.Objects;
import java.util.UUID;

import static io.vertx.ext.auth.mongo.MongoAuthorization.DEFAULT_USERNAME_FIELD;
import io.vertx.core.json.JsonObject;

/**
* This class represents a user.
*
* @author Armin Schnabel
* @version 1.0.1
* @version 2.0.0
* @since 1.0.0
*/
@SuppressWarnings("unused") // Part of the API (e.g. used by collector, incentives)
public class User {

/**
* The identifier of the {@link User}.
*/
private final ObjectId id;
private final UUID id;
/**
* The username of the {link User}.
*/
Expand All @@ -46,10 +47,10 @@ public class User {
/**
* Constructs a fully initialized instance of this class.
*
* @param id The identifier of the {@link User}.
* @param id The identifier of the {@link User}.
* @param name The username of the {link User}.
*/
public User(final ObjectId id, final String name) {
public User(final UUID id, final String name) {
this.id = id;
this.name = name;
}
Expand All @@ -60,15 +61,15 @@ public User(final ObjectId id, final String name) {
* @param databaseValue A role entry from the database.
*/
public User(final JsonObject databaseValue) {
this.id = new ObjectId(databaseValue.getString("_id"));
this.id = UUID.fromString(databaseValue.getString("_id"));
this.name = databaseValue.getString(DEFAULT_USERNAME_FIELD);
}

/**
* @return The identifier of the {@link User}.
*/
@SuppressWarnings("unused") // Part of the API
public ObjectId getId() {
public UUID getId() {
return id;
}

Expand Down Expand Up @@ -101,7 +102,7 @@ public boolean equals(Object o) {
return true;
if (o == null || getClass() != o.getClass())
return false;
User user = (User) o;
User user = (User)o;
return id.equals(user.id) && name.equals(user.name);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Cyface GmbH
* Copyright 2019-2023 Cyface GmbH
*
* This file is part of the Cyface API Library.
*
Expand All @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with the Cyface API Library. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.api;
package de.cyface.api.v2;

import static io.vertx.ext.auth.mongo.MongoAuthorization.DEFAULT_ROLE_FIELD;
import static io.vertx.ext.auth.mongo.MongoAuthorization.DEFAULT_USERNAME_FIELD;
Expand All @@ -27,12 +27,14 @@
import java.util.Set;
import java.util.stream.Collectors;

import de.cyface.api.DatabaseConstants;
import de.cyface.api.PauseAndResumeStrategy;
import de.cyface.api.v2.model.Role;
import de.cyface.api.v2.model.User;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.cyface.api.model.Role;
import de.cyface.api.model.User;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
Expand All @@ -48,7 +50,7 @@
* This class ensures that such requests are properly authorized.
*
* @author Armin Schnabel
* @version 2.0.2
* @version 2.0.3
* @since 1.0.0
*/
public abstract class Authorizer implements Handler<RoutingContext> {
Expand Down Expand Up @@ -161,7 +163,7 @@ public void handle(final RoutingContext context) {
* @param header the header of the request which may contain parameters required to process the request.
*/
protected abstract void handleAuthorizedRequest(final RoutingContext ctx, final User user, final Set<User> users,
final MultiMap header);
final MultiMap header);

/**
* Loads all users which the authenticated {@code User} can access.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 Cyface GmbH - All Rights Reserved
* Copyright (C) 2022-2023 Cyface GmbH - All Rights Reserved
*
* This file is part of the Cyface API Library.
*
Expand All @@ -16,12 +16,12 @@
* You should have received a copy of the GNU General Public License
* along with the Cyface API Library. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.api;
package de.cyface.api.v2;

import java.util.List;
import java.util.stream.Collectors;

import de.cyface.api.model.User;
import de.cyface.api.v2.model.User;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
Expand All @@ -32,7 +32,7 @@
* Loads {@link User}s from the database for a vert.x context.
*
* @author Armin Schnabel
* @version 1.0.0
* @version 1.0.1
* @since 1.0.0
*/
public class UserRetriever {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with the Cyface API Library. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.api.model;
package de.cyface.api.v2.model;

import java.util.Objects;

Expand All @@ -28,7 +28,7 @@
* This class represents a user role of a specific {@link Type}.
*
* @author Armin Schnabel
* @version 1.2.0
* @version 1.2.1
* @since 1.0.0
*/
public class Role {
Expand Down
112 changes: 112 additions & 0 deletions api/src/main/java/de/cyface/api/v2/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2022 Cyface GmbH
*
* This file is part of the Cyface API Library.
*
* The Cyface API Library 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 API Library 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 API Library. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.api.v2.model;

import io.vertx.core.json.JsonObject;
import org.bson.types.ObjectId;

import java.util.Objects;

import static io.vertx.ext.auth.mongo.MongoAuthorization.DEFAULT_USERNAME_FIELD;

/**
* This class represents a user.
*
* @author Armin Schnabel
* @version 1.0.1
* @since 1.0.0
*/
public class User {

/**
* The identifier of the {@link User}.
*/
private final ObjectId id;
/**
* The username of the {link User}.
*/
private final String name;

/**
* Constructs a fully initialized instance of this class.
*
* @param id The identifier of the {@link User}.
* @param name The username of the {link User}.
*/
public User(final ObjectId id, final String name) {
this.id = id;
this.name = name;
}

/**
* Constructs a fully initialized instance of this class.
*
* @param databaseValue A role entry from the database.
*/
public User(final JsonObject databaseValue) {
this.id = new ObjectId(databaseValue.getString("_id"));
this.name = databaseValue.getString(DEFAULT_USERNAME_FIELD);
}

/**
* @return The identifier of the {@link User}.
*/
@SuppressWarnings("unused") // Part of the API
public ObjectId getId() {
return id;
}

/**
* @return The identifier of the {@link User} as {@code String}.
*/
@SuppressWarnings("unused") // Part of the API
public String getIdString() {
return id.toString();
}

/**
* @return The username of the {link User}.
*/
public String getName() {
return name;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
User user = (User) o;
return id.equals(user.id) && name.equals(user.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}
}
Loading

0 comments on commit fb4ba1f

Please sign in to comment.