|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.api.auth; |
| 18 | + |
| 19 | +import com.cloud.api.ApiServlet; |
| 20 | +import com.cloud.api.response.ApiResponseSerializer; |
| 21 | +import com.cloud.domain.Domain; |
| 22 | +import com.cloud.user.Account; |
| 23 | +import com.cloud.user.User; |
| 24 | +import com.cloud.user.UserAccount; |
| 25 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 26 | +import org.apache.cloudstack.api.APICommand; |
| 27 | +import org.apache.cloudstack.api.ApiConstants; |
| 28 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 29 | +import org.apache.cloudstack.api.ApiServerService; |
| 30 | +import org.apache.cloudstack.api.BaseCmd; |
| 31 | +import org.apache.cloudstack.api.Parameter; |
| 32 | +import org.apache.cloudstack.api.ServerApiException; |
| 33 | +import org.apache.cloudstack.api.auth.APIAuthenticationType; |
| 34 | +import org.apache.cloudstack.api.auth.APIAuthenticator; |
| 35 | +import org.apache.cloudstack.api.auth.PluggableAPIAuthenticator; |
| 36 | +import org.apache.cloudstack.api.response.SuccessResponse; |
| 37 | +import org.jetbrains.annotations.Nullable; |
| 38 | + |
| 39 | +import javax.inject.Inject; |
| 40 | +import javax.servlet.http.HttpServletRequest; |
| 41 | +import javax.servlet.http.HttpServletResponse; |
| 42 | +import javax.servlet.http.HttpSession; |
| 43 | +import java.net.InetAddress; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | + |
| 47 | +@APICommand(name = "forgotPassword", |
| 48 | + description = "Sends an email to the user with a token to reset the password using resetPassword command.", |
| 49 | + requestHasSensitiveInfo = true, |
| 50 | + responseObject = SuccessResponse.class) |
| 51 | +public class DefaultForgotPasswordAPIAuthenticatorCmd extends BaseCmd implements APIAuthenticator { |
| 52 | + |
| 53 | + |
| 54 | + ///////////////////////////////////////////////////// |
| 55 | + //////////////// API parameters ///////////////////// |
| 56 | + ///////////////////////////////////////////////////// |
| 57 | + @Parameter(name = ApiConstants.USERNAME, type = CommandType.STRING, description = "Username", required = true) |
| 58 | + private String username; |
| 59 | + |
| 60 | + @Parameter(name = ApiConstants.DOMAIN, type = CommandType.STRING, description = "Path of the domain that the user belongs to. Example: domain=/com/cloud/internal. If no domain is passed in, the ROOT (/) domain is assumed.") |
| 61 | + private String domain; |
| 62 | + |
| 63 | + @Inject |
| 64 | + ApiServerService _apiServer; |
| 65 | + |
| 66 | + ///////////////////////////////////////////////////// |
| 67 | + /////////////////// Accessors /////////////////////// |
| 68 | + ///////////////////////////////////////////////////// |
| 69 | + |
| 70 | + public String getUsername() { |
| 71 | + return username; |
| 72 | + } |
| 73 | + |
| 74 | + public String getDomainName() { |
| 75 | + return domain; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + ///////////////////////////////////////////////////// |
| 80 | + /////////////// API Implementation/////////////////// |
| 81 | + ///////////////////////////////////////////////////// |
| 82 | + |
| 83 | + @Override |
| 84 | + public long getEntityOwnerId() { |
| 85 | + return Account.Type.NORMAL.ordinal(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void execute() throws ServerApiException { |
| 90 | + // We should never reach here |
| 91 | + throw new ServerApiException(ApiErrorCode.METHOD_NOT_ALLOWED, "This is an authentication api, cannot be used directly"); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String authenticate(String command, Map<String, Object[]> params, HttpSession session, InetAddress remoteAddress, String responseType, StringBuilder auditTrailSb, final HttpServletRequest req, final HttpServletResponse resp) throws ServerApiException { |
| 96 | + final String[] username = (String[])params.get(ApiConstants.USERNAME); |
| 97 | + final String[] domainName = (String[])params.get(ApiConstants.DOMAIN); |
| 98 | + |
| 99 | + Long domainId = null; |
| 100 | + String domain = null; |
| 101 | + domain = getDomainName(auditTrailSb, domainName, domain); |
| 102 | + |
| 103 | + String serializedResponse = null; |
| 104 | + if (username != null) { |
| 105 | + try { |
| 106 | + final Domain userDomain = _domainService.findDomainByPath(domain); |
| 107 | + if (userDomain != null) { |
| 108 | + domainId = userDomain.getId(); |
| 109 | + } else { |
| 110 | + throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Unable to find the domain from the path %s", domain)); |
| 111 | + } |
| 112 | + final UserAccount userAccount = _accountService.getActiveUserAccount(username[0], domainId); |
| 113 | + if (userAccount != null && List.of(User.Source.SAML2, User.Source.OAUTH2, User.Source.LDAP).contains(userAccount.getSource())) { |
| 114 | + throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Forgot Password is not allowed for this user"); |
| 115 | + } |
| 116 | + boolean success = _apiServer.forgotPassword(userAccount); |
| 117 | + SuccessResponse successResponse = new SuccessResponse(); |
| 118 | + successResponse.setSuccess(success); |
| 119 | + successResponse.setResponseName(getCommandName()); |
| 120 | + return ApiResponseSerializer.toSerializedString(successResponse, responseType); |
| 121 | + } catch (final CloudRuntimeException ex) { |
| 122 | + ApiServlet.invalidateHttpSession(session, "fall through to API key,"); |
| 123 | + String msg = String.format("%s", ex.getMessage() != null ? |
| 124 | + ex.getMessage() : |
| 125 | + "forgot password request failed for user, check if username/domain are correct"); |
| 126 | + auditTrailSb.append(" " + ApiErrorCode.ACCOUNT_ERROR + " " + msg); |
| 127 | + serializedResponse = _apiServer.getSerializedApiError(ApiErrorCode.ACCOUNT_ERROR.getHttpCode(), msg, params, responseType); |
| 128 | + if (logger.isTraceEnabled()) { |
| 129 | + logger.trace(msg); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + // We should not reach here and if we do we throw an exception |
| 134 | + throw new ServerApiException(ApiErrorCode.ACCOUNT_ERROR, serializedResponse); |
| 135 | + } |
| 136 | + |
| 137 | + @Nullable |
| 138 | + private String getDomainName(StringBuilder auditTrailSb, String[] domainName, String domain) { |
| 139 | + if (domainName != null) { |
| 140 | + domain = domainName[0]; |
| 141 | + auditTrailSb.append(" domain=" + domain); |
| 142 | + if (domain != null) { |
| 143 | + // ensure domain starts with '/' and ends with '/' |
| 144 | + if (!domain.endsWith("/")) { |
| 145 | + domain += '/'; |
| 146 | + } |
| 147 | + if (!domain.startsWith("/")) { |
| 148 | + domain = "/" + domain; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + return domain; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public APIAuthenticationType getAPIType() { |
| 157 | + return APIAuthenticationType.PASSWORD_RESET; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void setAuthenticators(List<PluggableAPIAuthenticator> authenticators) { |
| 162 | + } |
| 163 | +} |
0 commit comments