1/*
2 * Copyright 2008 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 *      http://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 */
16package org.mockftpserver.fake.command
17
18import org.mockftpserver.fake.command.AbstractFakeCommandHandler
19import org.mockftpserver.core.command.Command
20import org.mockftpserver.core.session.Session
21import org.mockftpserver.core.session.SessionKeys
22import org.mockftpserver.core.command.ReplyCodes
23
24/**
25 * CommandHandler for the PASS command. Handler logic:
26 * <ol>
27 *  <li>If the required pathname parameter is missing, then reply with 501</li>
28 *  <li>If this command was not preceded by a valid USER command, then reply with 503</li>
29 *  <li>If the named user does not exist, then reply with 530</li>
30 *  <li>If the specified password is not correct, then reply with 530</li>
31 *  <li>Otherwise, reply with 250</li>
32 * </ol>
33 *
34 * @version $Revision: $ - $Date: $
35 *
36 * @author Chris Mair
37 */
38class PassCommandHandler extends AbstractFakeCommandHandler {
39
40    protected void handle(Command command, Session session) {
41        def password = getRequiredParameter(command)
42        def username = getRequiredSessionAttribute(session, SessionKeys.USERNAME)
43
44        def userAccount = serverConfiguration.getUserAccount(username)
45        if (userAccount == null) {
46            sendReply(session, ReplyCodes.PASS_LOG_IN_FAILED)
47            return
48        }
49
50        if (userAccount.isValidPassword(password)) {
51            sendReply(session, ReplyCodes.PASS_OK)
52            session.setAttribute(SessionKeys.USER_ACCOUNT, userAccount)
53        }
54        else {
55            sendReply(session, ReplyCodes.PASS_LOG_IN_FAILED)
56        }
57    }
58
59}