Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;

import java.util.Collection;
import java.util.Set;
Expand All @@ -32,22 +33,22 @@ private DDSsoScopes() {
}

@NotNull
public static Set<String> parse(@Nullable String scope) {
public static Set<String> parse(@Nullable String scope) throws DBException {
if (scope == null || scope.isEmpty() || scope.length() > 4096) {
throw new IllegalArgumentException("OAuth scopes are required");
throw new DBException("OAuth scopes are required");
}
Set<String> scopes = new TreeSet<>();
for (String value : scope.split(" ", -1)) {
if (!TOKEN.matcher(value).matches()) {
throw new IllegalArgumentException("Invalid OAuth scope");
throw new DBException("Invalid OAuth scope");
}
scopes.add(value);
}
return Set.copyOf(scopes);
}

@NotNull
public static String format(@NotNull Collection<String> scopes) {
public static String format(@NotNull Collection<String> scopes) throws DBException {
String value = String.join(" ", new TreeSet<>(scopes));
parse(value);
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.assertj.core.api.Assertions;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
Expand All @@ -27,7 +28,7 @@

class DDSsoScopesTest {
@Test
void scopesAreCaseSensitiveAndHaveAStableRepresentation() {
void scopesAreCaseSensitiveAndHaveAStableRepresentation() throws DBException {
var scopes = DDSsoScopes.parse("dbeaver-storage:access account:access account:access");
Assertions.assertThat(scopes).isEqualTo(Set.of("dbeaver-storage:access", "account:access"));
Assertions.assertThat(DDSsoScopes.format(scopes)).isEqualTo("account:access dbeaver-storage:access");
Expand All @@ -39,6 +40,6 @@ void scopesAreCaseSensitiveAndHaveAStableRepresentation() {
@ValueSource(strings = {" ", " account:access", "account:access ", "account:access dbeaver-storage:access",
"account:access\tdbeaver-storage:access", "account:access\n", "a\"b", "a\\b", "права"})
void rejectsInvalidWireScopes(@Nullable String scope) {
Assertions.assertThatThrownBy(() -> DDSsoScopes.parse(scope)).isInstanceOf(IllegalArgumentException.class);
Assertions.assertThatThrownBy(() -> DDSsoScopes.parse(scope)).isInstanceOf(DBException.class);
}
}
Loading