summaryrefslogtreecommitdiffstats
path: root/gerrit-sshd/src/main/java/com/google/gerrit/sshd/DatabasePasswordAuth.java
blob: a346ab6087f896ec7f9821b7903519a6fb57f3c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.sshd;

import com.google.gerrit.reviewdb.AccountExternalId;
import com.google.gerrit.server.AccessPath;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.sshd.SshScope.Context;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

import org.apache.mina.core.future.IoFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.sshd.server.PasswordAuthenticator;
import org.apache.sshd.server.session.ServerSession;

import java.net.SocketAddress;

/**
 * Authenticates by password through {@link AccountExternalId} entities.
 */
@Singleton
class DatabasePasswordAuth implements PasswordAuthenticator {
  private final AccountCache accountCache;
  private final SshLog log;
  private final IdentifiedUser.GenericFactory userFactory;

  @Inject
  DatabasePasswordAuth(final AccountCache ac, final SshLog l,
      final IdentifiedUser.GenericFactory uf) {
    accountCache = ac;
    log = l;
    userFactory = uf;
  }

  @Override
  public boolean authenticate(final String username, final String password,
      final ServerSession session) {
    final SshSession sd = session.getAttribute(SshSession.KEY);

    AccountState state = accountCache.getByUsername(username);
    if (state == null) {
      sd.authenticationError(username, "user-not-found");
      return false;
    }

    final String p = state.getPassword(username);
    if (p == null) {
      sd.authenticationError(username, "no-password");
      return false;
    }

    if (!p.equals(password)) {
      sd.authenticationError(username, "incorrect-password");
      return false;
    }

    if (sd.getCurrentUser() == null) {
      sd.authenticationSuccess(username, createUser(sd, state));

      // If this is the first time we've authenticated this
      // session, record a login event in the log and add
      // a close listener to record a logout event.
      //
      Context ctx = new Context(sd, null);
      Context old = SshScope.set(ctx);
      try {
        log.onLogin();
      } finally {
        SshScope.set(old);
      }

      session.getIoSession().getCloseFuture().addListener(
          new IoFutureListener<IoFuture>() {
            @Override
            public void operationComplete(IoFuture future) {
              final Context ctx = new Context(sd, null);
              final Context old = SshScope.set(ctx);
              try {
                log.onLogout();
              } finally {
                SshScope.set(old);
              }
            }
          });
    }

    return true;
  }

  private IdentifiedUser createUser(final SshSession sd,
      final AccountState state) {
    return userFactory.create(AccessPath.SSH_COMMAND,
        new Provider<SocketAddress>() {
          @Override
          public SocketAddress get() {
            return sd.getRemoteAddress();
          }
        }, state.getAccount().getId());
  }
}