|
| 1 | +/* |
| 2 | + * Copyright 2026-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 | +package org.springframework.shell.samples.secure.input; |
| 17 | + |
| 18 | +import java.io.PrintWriter; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.shell.core.InputReader; |
| 24 | +import org.springframework.shell.core.command.CommandContext; |
| 25 | +import org.springframework.shell.core.command.annotation.Command; |
| 26 | +import org.springframework.shell.core.command.annotation.CommandGroup; |
| 27 | +import org.springframework.shell.core.command.availability.Availability; |
| 28 | +import org.springframework.shell.core.command.availability.AvailabilityProvider; |
| 29 | + |
| 30 | +@CommandGroup(name = "Authentication Commands", description = "Commands related to user authentication", |
| 31 | + prefix = "auth") |
| 32 | +public class AuthenticationCommands { |
| 33 | + |
| 34 | + private String username; |
| 35 | + |
| 36 | + private boolean authenticated = false; |
| 37 | + |
| 38 | + // In-memory user store for demonstration purposes |
| 39 | + private Map<String, String> userStore = new HashMap<>() { |
| 40 | + { |
| 41 | + put("foo", "bar"); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + @Command(name = "login", description = "Log in to the system") |
| 46 | + public void login(CommandContext commandContext) { |
| 47 | + InputReader inputReader = commandContext.inputReader(); |
| 48 | + PrintWriter outputWriter = commandContext.outputWriter(); |
| 49 | + if (this.authenticated) { |
| 50 | + outputWriter.println("Already logged in."); |
| 51 | + return; |
| 52 | + } |
| 53 | + try { |
| 54 | + String username = inputReader.readInput("Username: "); |
| 55 | + String password = new String(inputReader.readPassword("Password: ")); |
| 56 | + if (userStore.containsKey(username) && userStore.get(username).equals(password)) { |
| 57 | + outputWriter.println("Login successful."); |
| 58 | + this.authenticated = true; |
| 59 | + this.username = username; |
| 60 | + } |
| 61 | + else { |
| 62 | + outputWriter.println("Invalid credentials."); |
| 63 | + } |
| 64 | + } |
| 65 | + catch (Exception e) { |
| 66 | + outputWriter.println("Failed to login."); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Command(name = "logout", description = "Log out of the system") |
| 71 | + public void logout() { |
| 72 | + if (!this.authenticated) { |
| 73 | + System.out.println("Not logged in."); |
| 74 | + return; |
| 75 | + } |
| 76 | + System.out.println("Logging out..."); |
| 77 | + this.authenticated = false; |
| 78 | + this.username = null; |
| 79 | + } |
| 80 | + |
| 81 | + @Command(name = "status", description = "Check authentication status") |
| 82 | + public String status() { |
| 83 | + return authenticated ? "Logged in as " + this.username : "Not logged in."; |
| 84 | + } |
| 85 | + |
| 86 | + @Command(name = "change-password", description = "Change password", availabilityProvider = "authenticationProvider") |
| 87 | + public void changePassword(CommandContext commandContext) { |
| 88 | + InputReader inputReader = commandContext.inputReader(); |
| 89 | + PrintWriter outputWriter = commandContext.outputWriter(); |
| 90 | + try { |
| 91 | + char[] currentPassword = inputReader.readPassword("Enter current password: "); |
| 92 | + if (!userStore.containsKey(this.username) |
| 93 | + || !userStore.get(this.username).equals(new String(currentPassword))) { |
| 94 | + outputWriter.println("Current password is incorrect."); |
| 95 | + return; |
| 96 | + } |
| 97 | + char[] chars = inputReader.readPassword("Enter new password: "); |
| 98 | + String newPassword = new String(chars); |
| 99 | + userStore.put(this.username, newPassword); |
| 100 | + outputWriter.println("Password successfully updated."); |
| 101 | + } |
| 102 | + catch (Exception e) { |
| 103 | + outputWriter.println("Failed to set password."); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Bean |
| 108 | + public AvailabilityProvider authenticationProvider() { |
| 109 | + return () -> this.authenticated ? Availability.available() |
| 110 | + : Availability.unavailable("You must be logged in to use this command."); |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments