1 package eu.fbk.knowledgestore.server.http; 2 3 import java.net.URL; 4 import java.util.Arrays; 5 import java.util.Set; 6 7 import javax.annotation.Nullable; 8 9 import com.google.common.base.Joiner; 10 import com.google.common.base.Objects; 11 import com.google.common.base.Preconditions; 12 import com.google.common.collect.ImmutableSet; 13 14 import eu.fbk.knowledgestore.internal.Util; 15 16 public final class SecurityConfig { 17 18 public static final String ROLE_DOWNLOADER = "downloader"; 19 20 public static final String ROLE_CRUD_READER = "crud_reader"; 21 22 public static final String ROLE_SPARQL_READER = "sparql_reader"; 23 24 public static final String ROLE_WRITER = "writer"; 25 26 public static final String ROLE_UI_USER = "ui_user"; 27 28 static final Set<String> ALL_ROLES = ImmutableSet.of(ROLE_DOWNLOADER, ROLE_CRUD_READER, 29 ROLE_SPARQL_READER, ROLE_WRITER, ROLE_UI_USER); 30 31 @Nullable 32 private final String realm; 33 34 private final String userdbLocation; 35 36 private final URL userdbURL; 37 38 private final Set<String> anonymousRoles; 39 40 public SecurityConfig(@Nullable final String realm, final String userdbLocation, 41 final String... anonymousRoles) { 42 this(realm, userdbLocation, Arrays.asList(anonymousRoles)); 43 } 44 45 public SecurityConfig(@Nullable final String realm, final String userdbLocation, 46 final Iterable<? extends String> anonymousRoles) { 47 this.realm = realm; 48 this.userdbLocation = Preconditions.checkNotNull(userdbLocation); 49 this.userdbURL = Util.getURL(userdbLocation); 50 this.anonymousRoles = ImmutableSet.copyOf(anonymousRoles); 51 for (final String role : anonymousRoles) { 52 Preconditions.checkArgument(ALL_ROLES.contains(role), "Invalid role %s", role); 53 } 54 } 55 56 @Nullable 57 public String getRealm() { 58 return this.realm; 59 } 60 61 public String getUserdbLocation() { 62 return this.userdbLocation; 63 } 64 65 public URL getUserdbURL() { 66 return this.userdbURL; 67 } 68 69 public Set<String> getAnonymousRoles() { 70 return this.anonymousRoles; 71 } 72 73 @Override 74 public boolean equals(final Object object) { 75 if (object == this) { 76 return true; 77 } 78 if (!(object instanceof SecurityConfig)) { 79 return false; 80 } 81 final SecurityConfig o = (SecurityConfig) object; 82 return Objects.equal(this.realm, o.realm) && this.userdbLocation.equals(o.userdbLocation) 83 && this.anonymousRoles.equals(o.anonymousRoles); 84 } 85 86 @Override 87 public int hashCode() { 88 return Objects.hashCode(this.realm, this.userdbLocation, this.anonymousRoles); 89 } 90 91 @Override 92 public String toString() { 93 final StringBuilder builder = new StringBuilder(); 94 if (this.realm != null) { 95 builder.append("realm=").append(this.realm).append(", "); 96 } 97 builder.append("userdb=").append(this.userdbLocation); 98 builder.append(", anonymousRoles=").append(Joiner.on(";").join(this.anonymousRoles)); 99 return super.toString(); 100 } 101 102 }