Repository: jenkinsci/jenkins
File: test/src/test/java/test/security/realm/InMemorySecurityRealm.java (Java)

Code before:
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package test.security.realm;

import hudson.security.AbstractPasswordBasedSecurityRealm;
import hudson.security.GroupDetails;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 *  This class provides an in-memory implementation of Jenkin's SecurityRealm for
 *  testing.
 *  <p>
 *  It allows the creation and removal of users, and accepts any password for authentication.
 *  It maintains an in-memory storage of user accounts in a HashMap {@link #userStorage} and
 *  allows for creation and deletion of users by modifying this hashmap.
 *  <p>
 *  The user also gets authenticated with any password as the only thing validated is presence
 *  of username entry in the hashmap.
 *  It implements the {@link #loadUserByUsername2(String)} method which simply returns the user from the
 *  in memory storage based on username passed.
 *  <p>
 *  A dummy implementation for {@link InMemorySecurityRealm} is provided in class {@link InMemorySecurityRealm}
 *  which contains no roles and only username is returned.
 *
 */
public class InMemorySecurityRealm extends AbstractPasswordBasedSecurityRealm {
    private Map<String, UserDetails> userStorage = new HashMap<>();

    public synchronized void createAccount(String username) {
        if (userStorage.containsKey(username)) {
            return;
        }
        UserDetails details = new InMemoryUserDetails(username);
        userStorage.put(username, details);
    }

Review comment (security):
```suggestion
 *  This class provides an in-memory implementation of Jenkins's {@link SecurityRealm} for
```

You resolved my previous comment correcting `Jenkin` without addressing the suggestion, please don't do that.

Wrt. the `@link`, check in IDE first to ensure this suggestion is correct.

Code after:
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package test.security.realm;

import hudson.security.AbstractPasswordBasedSecurityRealm;
import hudson.security.GroupDetails;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 *  This class provides an in-memory implementation of Jenkins's {@link hudson.security.SecurityRealm} for
 *  testing.
 *  <p>
 *  It allows the creation and removal of users, and accepts any password for authentication.
 *  It maintains an in-memory storage of user accounts.
 *  <p>
 *  The user also gets authenticated with any password as the only thing validated is presence
 *  of username entry in the hashmap.
 *  The {@link #createAccount(String)} is used to populate the in-memory storage.
 *  It implements the {@link #loadUserByUsername2(String)} method which simply returns the user from the
 *  in memory storage based on username passed.
 *  <p>
 *  A dummy implementation for {@link InMemorySecurityRealm} is provided in class {@link InMemorySecurityRealm}
 *  which contains no roles and only username is returned.
 *
 */
public class InMemorySecurityRealm extends AbstractPasswordBasedSecurityRealm {
    private Map<String, UserDetails> userStorage = new HashMap<>();

    public synchronized void createAccount(String username) {
        if (userStorage.containsKey(username)) {
            return;
        }
        UserDetails details = new InMemoryUserDetails(username);
        userStorage.put(username, details);
    }