19d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/*
29d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Copyright 2008 the original author or authors.
39d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
49d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Licensed under the Apache License, Version 2.0 (the "License");
59d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * you may not use this file except in compliance with the License.
69d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * You may obtain a copy of the License at
79d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
89d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *      http://www.apache.org/licenses/LICENSE-2.0
99d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
109d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * Unless required by applicable law or agreed to in writing, software
119d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * distributed under the License is distributed on an "AS IS" BASIS,
129d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * See the License for the specific language governing permissions and
149d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * limitations under the License.
159d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
169d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairpackage org.mockftpserver.fake.command
179d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
189d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.fake.command.AbstractFakeCommandHandler
199d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.Command
209d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.session.Session
219d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.session.SessionKeys
229d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairimport org.mockftpserver.core.command.ReplyCodes
239d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
249d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair/**
259d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * CommandHandler for the PASS command. Handler logic:
269d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * <ol>
279d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *  <li>If the required pathname parameter is missing, then reply with 501</li>
289d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *  <li>If this command was not preceded by a valid USER command, then reply with 503</li>
299d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *  <li>If the named user does not exist, then reply with 530</li>
309d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *  <li>If the specified password is not correct, then reply with 530</li>
319d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *  <li>Otherwise, reply with 250</li>
329d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * </ol>
339d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
349d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @version $Revision: $ - $Date: $
359d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair *
369d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair * @author Chris Mair
379d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair */
389d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismairclass PassCommandHandler extends AbstractFakeCommandHandler {
399d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
409d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair    protected void handle(Command command, Session session) {
419d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        def password = getRequiredParameter(command)
429d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        def username = getRequiredSessionAttribute(session, SessionKeys.USERNAME)
439d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
449d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        def userAccount = serverConfiguration.getUserAccount(username)
459d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        if (userAccount == null) {
469d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            sendReply(session, ReplyCodes.PASS_LOG_IN_FAILED)
479d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            return
489d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
499d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair
509d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        if (userAccount.isValidPassword(password)) {
519d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            sendReply(session, ReplyCodes.PASS_OK)
529d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            session.setAttribute(SessionKeys.USER_ACCOUNT, userAccount)
539d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
549d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        else {
559d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair            sendReply(session, ReplyCodes.PASS_LOG_IN_FAILED)
569d9aece7b2c2865253fdd2946a4d11a4f642c5aechrismair        }
57    }
58
59}