|
| 1 | +/* |
| 2 | + * Copyright 2004-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.core; |
| 18 | + |
| 19 | +import java.io.Serial; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.LinkedHashSet; |
| 22 | +import java.util.function.Consumer; |
| 23 | + |
| 24 | +import org.jspecify.annotations.Nullable; |
| 25 | + |
| 26 | +@Transient |
| 27 | +final class SimpleAuthentication implements Authentication { |
| 28 | + |
| 29 | + @Serial |
| 30 | + private static final long serialVersionUID = 3194696462184782814L; |
| 31 | + |
| 32 | + private final @Nullable Object principal; |
| 33 | + |
| 34 | + private final @Nullable Object credentials; |
| 35 | + |
| 36 | + private final Collection<GrantedAuthority> authorities; |
| 37 | + |
| 38 | + private final @Nullable Object details; |
| 39 | + |
| 40 | + private final boolean authenticated; |
| 41 | + |
| 42 | + private SimpleAuthentication(Builder builder) { |
| 43 | + this.principal = builder.principal; |
| 44 | + this.credentials = builder.credentials; |
| 45 | + this.authorities = builder.authorities; |
| 46 | + this.details = builder.details; |
| 47 | + this.authenticated = builder.authenticated; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Collection<? extends GrantedAuthority> getAuthorities() { |
| 52 | + return this.authorities; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public @Nullable Object getCredentials() { |
| 57 | + return this.credentials; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public @Nullable Object getDetails() { |
| 62 | + return this.details; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public @Nullable Object getPrincipal() { |
| 67 | + return this.principal; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean isAuthenticated() { |
| 72 | + return this.authenticated; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { |
| 77 | + throw new IllegalArgumentException( |
| 78 | + "Instead of calling this setter, please call toBuilder to create a new instance"); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String getName() { |
| 83 | + return (this.principal == null) ? "" : this.principal.toString(); |
| 84 | + } |
| 85 | + |
| 86 | + static final class Builder implements Authentication.Builder<Object, Object, Builder> { |
| 87 | + |
| 88 | + private final Collection<GrantedAuthority> authorities = new LinkedHashSet<>(); |
| 89 | + |
| 90 | + private @Nullable Object principal; |
| 91 | + |
| 92 | + private @Nullable Object credentials; |
| 93 | + |
| 94 | + private @Nullable Object details; |
| 95 | + |
| 96 | + private boolean authenticated; |
| 97 | + |
| 98 | + Builder(Authentication authentication) { |
| 99 | + this.authorities.addAll(authentication.getAuthorities()); |
| 100 | + this.principal = authentication.getPrincipal(); |
| 101 | + this.credentials = authentication.getCredentials(); |
| 102 | + this.details = authentication.getDetails(); |
| 103 | + this.authenticated = authentication.isAuthenticated(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Builder authorities(Consumer<Collection<GrantedAuthority>> authorities) { |
| 108 | + authorities.accept(this.authorities); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Builder details(@Nullable Object details) { |
| 114 | + this.details = details; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Builder principal(@Nullable Object principal) { |
| 120 | + this.principal = principal; |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Builder credentials(@Nullable Object credentials) { |
| 126 | + this.credentials = credentials; |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Builder authenticated(boolean authenticated) { |
| 132 | + this.authenticated = authenticated; |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Authentication build() { |
| 138 | + return new SimpleAuthentication(this); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments