Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
28c40be0
Unverified
Commit
28c40be0
authored
Mar 09, 2021
by
Gerard Krupa
Browse files
#665 - [Feature Request] Get PGP keys in user API
parent
750213b9
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/UserApi.java
View file @
28c40be0
...
...
@@ -14,6 +14,7 @@ import javax.ws.rs.core.Response;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.models.CustomAttribute
;
import
org.gitlab4j.api.models.Email
;
import
org.gitlab4j.api.models.GpgKey
;
import
org.gitlab4j.api.models.ImpersonationToken
;
import
org.gitlab4j.api.models.ImpersonationToken.Scope
;
import
org.gitlab4j.api.models.SshKey
;
...
...
@@ -522,7 +523,7 @@ public class UserApi extends AbstractApi {
* Either password or resetPassword should be specified (resetPassword takes priority).</p>
*
* <pre><code>GitLab Endpoint: POST /users</code></pre>
*
*
* <p>The following properties of the provided User instance can be set during creation:<pre><code> email (required) - Email
* username (required) - Username
* name (required) - Name
...
...
@@ -1211,4 +1212,85 @@ public class UserApi extends AbstractApi {
public
void
deleteEmail
(
final
Object
userIdOrUsername
,
final
Long
emailId
)
throws
GitLabApiException
{
delete
(
Response
.
Status
.
NO_CONTENT
,
null
,
"users"
,
getUserIdOrUsername
(
userIdOrUsername
),
"emails"
,
emailId
);
}
/**
* Get all GPG keys for the current user.
*
* <pre><code>GitLab Endpoint: GET /user/gpg_keys</code></pre>
*
* @throws GitLabApiException if any exception occurs
*/
public
List
<
GpgKey
>
listGpgKeys
()
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"user"
,
"gpg_keys"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
GpgKey
>>()
{}));
}
/**
* Add a GPG key for the current user
*
* <pre><code>GitLab Endpoint: POST /user/gpg_keys</code></pre>
*
* @param key the ASCII-armored exported public GPG key to add
* @throws GitLabApiException if any exception occurs
*/
public
GpgKey
addGpgKey
(
final
String
key
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
()
.
withParam
(
"key"
,
key
,
true
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"user"
,
"gpg_keys"
);
return
(
response
.
readEntity
(
GpgKey
.
class
));
}
/**
* Remove a specific GPG key for the current user
*
* <pre><code>GitLab Endpoint: DELETE /user/gpg_keys/:keyId</code></pre>
*
* @param keyId the key ID in the form if an Integer(ID)
* @throws GitLabApiException if any exception occurs
*/
public
void
deleteGpgKey
(
final
Integer
keyId
)
throws
GitLabApiException
{
delete
(
Response
.
Status
.
NO_CONTENT
,
null
,
"user"
,
"gpg_keys"
,
keyId
);
}
/**
* Get all GPG keys for a given user.
*
* <pre><code>GitLab Endpoint: GET /users/:id/gpg_keys</code></pre>
*
* @param userId the user in the form of an Integer(ID)
* @throws GitLabApiException if any exception occurs
*/
public
List
<
GpgKey
>
listGpgKeys
(
final
Integer
userId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"users"
,
userId
,
"gpg_keys"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
GpgKey
>>()
{}));
}
/**
* Add a GPG key for a specific user
*
* <pre><code>GitLab Endpoint: POST /users/:id/gpg_keys</code></pre>
*
* @param userId the user in the form of an Integer(ID)
* @param key the ASCII-armored exported public GPG key to add
* @throws GitLabApiException if any exception occurs
*/
public
GpgKey
addGpgKey
(
final
Integer
userId
,
final
String
key
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
()
.
withParam
(
"key"
,
key
,
true
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"users"
,
userId
,
"gpg_keys"
);
return
(
response
.
readEntity
(
GpgKey
.
class
));
}
/**
* Remove a specific GPG key for a specific user
*
* <pre><code>GitLab Endpoint: DELETE /users/:id/gpg_keys/:keyId</code></pre>
*
* @param userId the user in the form of an Integer(ID)
* @param keyId the key ID in the form if an Integer(ID)
* @throws GitLabApiException if any exception occurs
*/
public
void
deleteGpgKey
(
final
Integer
userId
,
final
Integer
keyId
)
throws
GitLabApiException
{
delete
(
Response
.
Status
.
NO_CONTENT
,
null
,
"users"
,
userId
,
"gpg_keys"
,
keyId
);
}
}
src/main/java/org/gitlab4j/api/models/GpgKey.java
0 → 100644
View file @
28c40be0
package
org.gitlab4j.api.models
;
import
java.util.Date
;
public
class
GpgKey
{
private
Integer
id
;
private
String
key
;
private
Date
created_at
;
public
Integer
getId
()
{
return
id
;
}
public
void
setId
(
Integer
id
)
{
this
.
id
=
id
;
}
public
String
getKey
()
{
return
key
;
}
public
void
setKey
(
String
key
)
{
this
.
key
=
key
;
}
public
Date
getCreated_at
()
{
return
created_at
;
}
public
void
setCreated_at
(
Date
created_at
)
{
this
.
created_at
=
created_at
;
}
}
src/test/java/org/gitlab4j/api/TestUserApi.java
View file @
28c40be0
...
...
@@ -19,6 +19,7 @@ import java.util.Optional;
import
javax.ws.rs.core.Response
;
import
org.gitlab4j.api.models.Email
;
import
org.gitlab4j.api.models.GpgKey
;
import
org.gitlab4j.api.models.ImpersonationToken
;
import
org.gitlab4j.api.models.ImpersonationToken.Scope
;
import
org.gitlab4j.api.models.SshKey
;
...
...
@@ -57,7 +58,7 @@ public class TestUserApi extends AbstractIntegrationTest {
private
static
final
String
TEST_SUDO_AS_USERNAME
=
HelperUtils
.
getProperty
(
SUDO_AS_USERNAME_KEY
);
private
static
final
String
TEST_IMPERSONATION_TOKEN_NAME
=
"token1"
;
private
static
final
String
TEST_SSH_KEY
=
private
static
final
String
TEST_SSH_KEY
=
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3rWzl/oPAD+Em2iGTmR81HcYZsopvnKp7jelI4XS91fT1NjCRrGsxf5Mw/"
+
"KnmtBjhk+kQjkhIrnsBDcs6DZWtNcHJtyWJZrYsfxMTqWCaQv+OTRwVboqS2pmPcbK3gizUd5GCLFTKbg4OMpdywTwi6NAPwQ"
+
"rtn3xwiVnGGCfBSyRFppcYP81otALctrlAW57V5+bQwFIJteJ+NWe1UmPxrqQ0N/a+dEEoJHzwX8RtVSkULafrRw8avn6Zp2x"
+
...
...
@@ -65,6 +66,58 @@ public class TestUserApi extends AbstractIntegrationTest {
private
static
final
String
TEST_USER_EMAIL
=
"test-user-email123@gitlab4j.org"
;
// Key for fake@fake.com - set to never expire
private
static
final
String
TEST_GPG_KEY
=
"-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n"
+
"mQINBGBHltIBEADW8zSGAs/XEkwWI36xOusuOqSINhTDVuqq3n50Oazb+a9Ai/MM\n"
+
"8GTm900ZZghGBVUXGm8PkWuSmabcpbDbjYmJx8aIY71UipXxXrfBzr8S2yOx7IjU\n"
+
"m6AEw0HwNUF6ayBwsklUFFWMyCeCVSCeZNldFwKRQApP6YOlTlwmZFESv32J6AHz\n"
+
"goeEsIcoea484nVJKOl7unneb8TyuF6kmViyZtiDkjeiH5vNy8XjSWH9xl5tcBxy\n"
+
"70NxkZt9EKnMq8izy51OBdzA+oWByGIGRjRPrW+5niMCGltV0w12M0uMDa2pJU2B\n"
+
"Z0U7uL/Lj3srMnD54OjbjK++wtYbshhGKXhAzshk9RgZq5fEN7Jjn1CTvue5EcHz\n"
+
"D27RD4yy35MledJ0hrvcTVVxvFmTg3TfDFdQBVLHjRATdXo7xT1Wg35M3z3aVSRt\n"
+
"PoynOxGNSotKUGfW5bhCB9XjUNpNY7+IphLS4LuQ3vZdEs9MTTWagoOoDx5w2PRS\n"
+
"7VNccRsqgIbNkpPjy78wN9m1QV97ytFs57eE+FfNDkKYeeCQDeHbeBlOmoEP/vSc\n"
+
"plOb6K3mdJgs0d5klXTOrFRVCYHHQ84p1YyQDKZO2Qd6JtHo5FNeqvgj5JwnBdfH\n"
+
"NGUdnaSn6hQTd8UB0AfwB+CC7cJq/fhbgcNvfK0Er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wzIBnmaZXQxdx4LiRy9TcaNtcowH4\n"
+
"U2yxCoG0o0kS7sS/rI77TV6WZ46DPCJmlNJ+MP8lt0j/nsDA3AECB1AA+8SNepbA\n"
+
"LSZY7MJmh4nsqJ+iy/XMosipluZx2u6ZwlXAHxAzHhs7FBsvdMtq/gLNAlZzVyeH\n"
+
"UqzRaMJps7xIbap5d5jZT5jaZwFeGi+63YVRx3Jm6dkiBCPFffLyWdrzkFTZdWqZ\n"
+
"PkiRbJ64wYPIWQgAN/RhmCcRBhxJE8f7kgo/nBkZ5dwmfXgnXpheEaaCSzvJ4nMh\n"
+
"TUdg6ZLna12QndjI5gy5aenrr5H/HmDKKSNkuWZv0+NS4GhwnL8NFs+MRk6znpLN\n"
+
"aEjPdfYxINCMz+uotKJV9NieDWIbEJLlfZUf2hJwuwwjQGAyVf7b\n"
+
"=ryCD\n"
+
"-----END PGP PUBLIC KEY BLOCK-----"
;
private
static
final
String
TEST_EXTERNAL_USERNAME
=
HelperUtils
.
getProperty
(
EXTERNAL_USERNAME_KEY
);
private
static
final
String
TEST_EXTERNAL_PROVIDER
=
HelperUtils
.
getProperty
(
EXTERNAL_PROVIDER_KEY
);
private
static
final
String
TEST_EXTERNAL_UID
=
HelperUtils
.
getProperty
(
EXTERNAL_UID_KEY
);
...
...
@@ -150,7 +203,7 @@ public class TestUserApi extends AbstractIntegrationTest {
assertNotNull
(
version
.
getVersion
());
assertNotNull
(
version
.
getRevision
());
}
@Test
public
void
testGetCurrentUser
()
throws
GitLabApiException
{
User
currentUser
=
gitLabApi
.
getUserApi
().
getCurrentUser
();
...
...
@@ -268,7 +321,7 @@ public class TestUserApi extends AbstractIntegrationTest {
User
user
=
gitLabApi
.
getUserApi
().
getCurrentUser
();
// NOTE: READ_REGISTRY scope is left out because the GitLab server docker instance does not have the
// NOTE: READ_REGISTRY scope is left out because the GitLab server docker instance does not have the
// registry configured and the test would thus fail.
Scope
[]
scopes
=
{
Scope
.
API
,
Scope
.
READ_USER
,
Scope
.
READ_REPOSITORY
,
Scope
.
WRITE_REPOSITORY
,
Scope
.
SUDO
};
Date
expiresAt
=
ISO8601
.
toDate
(
"2018-01-01T00:00:00Z"
);
...
...
@@ -440,11 +493,51 @@ public class TestUserApi extends AbstractIntegrationTest {
assertTrue
(
emails
.
size
()
==
currentSize
+
1
);
Email
found
=
emails
.
stream
().
filter
(
e
->
e
.
getEmail
().
equals
(
TEST_USER_EMAIL
)).
findAny
().
orElse
(
null
);
assertNotNull
(
found
);
gitLabApi
.
getUserApi
().
deleteEmail
(
currentUser
,
email
.
getId
());
emails
=
gitLabApi
.
getUserApi
().
getEmails
(
currentUser
);
assertEquals
(
currentSize
,
emails
.
size
());
found
=
emails
.
stream
().
filter
(
e
->
e
.
getEmail
().
equals
(
TEST_USER_EMAIL
)).
findAny
().
orElse
(
null
);
assertNull
(
found
);
}
@Test
public
void
testGpgKeys
()
throws
GitLabApiException
{
User
currentUser
=
gitLabApi
.
getUserApi
().
getCurrentUser
();
assertNotNull
(
currentUser
);
List
<
GpgKey
>
keys
=
gitLabApi
.
getUserApi
().
listGpgKeys
(
currentUser
.
getId
());
assertNotNull
(
keys
);
int
currentSize
=
keys
.
size
();
GpgKey
key
=
gitLabApi
.
getUserApi
().
addGpgKey
(
currentUser
.
getId
(),
TEST_GPG_KEY
);
keys
=
gitLabApi
.
getUserApi
().
listGpgKeys
(
currentUser
.
getId
());
assertTrue
(
keys
.
size
()
==
currentSize
+
1
);
GpgKey
found
=
keys
.
stream
().
filter
(
e
->
e
.
getKey
().
equals
(
TEST_GPG_KEY
)).
findAny
().
orElse
(
null
);
assertNotNull
(
found
);
gitLabApi
.
getUserApi
().
deleteGpgKey
(
currentUser
.
getId
(),
key
.
getId
());
keys
=
gitLabApi
.
getUserApi
().
listGpgKeys
(
currentUser
.
getId
());
assertEquals
(
currentSize
,
keys
.
size
());
found
=
keys
.
stream
().
filter
(
e
->
e
.
getKey
().
equals
(
TEST_GPG_KEY
)).
findAny
().
orElse
(
null
);
assertNull
(
found
);
}
@Test
public
void
testGpgKeysCurrentUser
()
throws
GitLabApiException
{
List
<
GpgKey
>
keys
=
gitLabApi
.
getUserApi
().
listGpgKeys
();
assertNotNull
(
keys
);
int
currentSize
=
keys
.
size
();
GpgKey
key
=
gitLabApi
.
getUserApi
().
addGpgKey
(
TEST_GPG_KEY
);
keys
=
gitLabApi
.
getUserApi
().
listGpgKeys
();
assertTrue
(
keys
.
size
()
==
currentSize
+
1
);
GpgKey
found
=
keys
.
stream
().
filter
(
e
->
e
.
getKey
().
equals
(
TEST_GPG_KEY
)).
findAny
().
orElse
(
null
);
assertNotNull
(
found
);
gitLabApi
.
getUserApi
().
deleteGpgKey
(
key
.
getId
());
keys
=
gitLabApi
.
getUserApi
().
listGpgKeys
();
assertEquals
(
currentSize
,
keys
.
size
());
found
=
keys
.
stream
().
filter
(
e
->
e
.
getKey
().
equals
(
TEST_GPG_KEY
)).
findAny
().
orElse
(
null
);
assertNull
(
found
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment