Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.account; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.common.collect.Lists; 19 : import com.google.gerrit.extensions.common.SshKeyInfo; 20 : import com.google.gerrit.extensions.restapi.AuthException; 21 : import com.google.gerrit.extensions.restapi.Response; 22 : import com.google.gerrit.extensions.restapi.RestReadView; 23 : import com.google.gerrit.server.CurrentUser; 24 : import com.google.gerrit.server.IdentifiedUser; 25 : import com.google.gerrit.server.account.AccountResource; 26 : import com.google.gerrit.server.account.AccountSshKey; 27 : import com.google.gerrit.server.account.VersionedAuthorizedKeys; 28 : import com.google.gerrit.server.permissions.GlobalPermission; 29 : import com.google.gerrit.server.permissions.PermissionBackend; 30 : import com.google.gerrit.server.permissions.PermissionBackendException; 31 : import com.google.inject.Inject; 32 : import com.google.inject.Provider; 33 : import com.google.inject.Singleton; 34 : import java.io.IOException; 35 : import java.util.List; 36 : import org.eclipse.jgit.errors.ConfigInvalidException; 37 : import org.eclipse.jgit.errors.RepositoryNotFoundException; 38 : 39 : /** 40 : * REST endpoint to list the SSH keys of an account. 41 : * 42 : * <p>This REST endpoint handles {@code GET /accounts/<account-identifier>/sshkeys/} requests. 43 : */ 44 : @Singleton 45 : public class GetSshKeys implements RestReadView<AccountResource> { 46 : 47 : private final Provider<CurrentUser> self; 48 : private final PermissionBackend permissionBackend; 49 : private final VersionedAuthorizedKeys.Accessor authorizedKeys; 50 : 51 : @Inject 52 : GetSshKeys( 53 : Provider<CurrentUser> self, 54 : PermissionBackend permissionBackend, 55 148 : VersionedAuthorizedKeys.Accessor authorizedKeys) { 56 148 : this.self = self; 57 148 : this.permissionBackend = permissionBackend; 58 148 : this.authorizedKeys = authorizedKeys; 59 148 : } 60 : 61 : @Override 62 : public Response<List<SshKeyInfo>> apply(AccountResource rsrc) 63 : throws AuthException, IOException, ConfigInvalidException, PermissionBackendException { 64 2 : if (!self.get().hasSameAccountId(rsrc.getUser())) { 65 1 : permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT); 66 : } 67 2 : return Response.ok(apply(rsrc.getUser())); 68 : } 69 : 70 : public List<SshKeyInfo> apply(IdentifiedUser user) 71 : throws RepositoryNotFoundException, IOException, ConfigInvalidException { 72 2 : return Lists.transform(authorizedKeys.getKeys(user.getAccountId()), GetSshKeys::newSshKeyInfo); 73 : } 74 : 75 : public static SshKeyInfo newSshKeyInfo(AccountSshKey sshKey) { 76 6 : SshKeyInfo info = new SshKeyInfo(); 77 6 : info.seq = sshKey.seq(); 78 6 : info.sshPublicKey = sshKey.sshPublicKey(); 79 6 : info.encodedKey = sshKey.encodedKey(); 80 6 : info.algorithm = sshKey.algorithm(); 81 6 : info.comment = Strings.emptyToNull(sshKey.comment()); 82 6 : info.valid = sshKey.valid(); 83 6 : return info; 84 : } 85 : }