Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
b6244581
Commit
b6244581
authored
Sep 21, 2012
by
Nihad Abbasov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
API: SSH keys belong to user entity
parent
4a072be2
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
195 additions
and
212 deletions
+195
-212
doc/api/keys.md
doc/api/keys.md
+0
-79
doc/api/users.md
doc/api/users.md
+78
-0
lib/api.rb
lib/api.rb
+0
-1
lib/api/entities.rb
lib/api/entities.rb
+2
-2
lib/api/keys.rb
lib/api/keys.rb
+0
-50
lib/api/users.rb
lib/api/users.rb
+53
-6
spec/requests/api/ssh_keys_spec.rb
spec/requests/api/ssh_keys_spec.rb
+0
-73
spec/requests/api/users_spec.rb
spec/requests/api/users_spec.rb
+62
-1
No files found.
doc/api/keys.md
deleted
100644 → 0
View file @
4a072be2
## List keys
Get a list of currently authenticated user's keys.
```
GET /keys
```
```
json
[
{
"id"
:
1
,
"title"
:
"Public key"
"key"
:
"ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4
596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4
soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0="
,
},
{
"id"
:
3
,
"title"
:
"Another Public key"
"key"
:
"ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4
596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4
soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0="
}
]
```
## Single key
Get a single key.
```
GET /keys/:id
```
Parameters:
+
`id`
(required) - The ID of a key
```
json
{
"id"
:
1
,
"title"
:
"Public key"
"key"
:
"ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4
596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4
soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0="
}
```
## Add key
Create new key owned by currently authenticated user
```
POST /keys
```
Parameters:
+
`title`
(required) - new SSH Key's title
+
`key`
(required) - new SSH key
Will return created key with status
`201 Created`
on success, or
`404 Not
found`
on fail.
## Delete key
Delete key owned by currently authenticated user
```
DELETE /keys/:id
```
Parameters:
+
`id`
(required) - key ID
Will return
`200 OK`
on success, or
`404 Not Found`
on fail.
doc/api/users.md
View file @
b6244581
...
...
@@ -88,3 +88,81 @@ GET /user
"theme_id"
:
1
}
```
## List SSH keys
Get a list of currently authenticated user's SSH keys.
```
GET /user/keys
```
```
json
[
{
"id"
:
1
,
"title"
:
"Public key"
"key"
:
"ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4
596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4
soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0="
,
},
{
"id"
:
3
,
"title"
:
"Another Public key"
"key"
:
"ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4
596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4
soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0="
}
]
```
## Single SSH key
Get a single key.
```
GET /user/keys/:id
```
Parameters:
+
`id`
(required) - The ID of an SSH key
```
json
{
"id"
:
1
,
"title"
:
"Public key"
"key"
:
"ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4
596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4
soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0="
}
```
## Add SSH key
Create new key owned by currently authenticated user
```
POST /user/keys
```
Parameters:
+
`title`
(required) - new SSH Key's title
+
`key`
(required) - new SSH key
Will return created key with status
`201 Created`
on success, or
`404 Not
found`
on fail.
## Delete SSH key
Delete key owned by currently authenticated user
```
DELETE /user/keys/:id
```
Parameters:
+
`id`
(required) - SSH key ID
Will return
`200 OK`
on success, or
`404 Not Found`
on fail.
lib/api.rb
View file @
b6244581
...
...
@@ -17,7 +17,6 @@ module Gitlab
mount
Projects
mount
Issues
mount
Milestones
mount
Keys
mount
Session
end
end
lib/api/entities.rb
View file @
b6244581
...
...
@@ -60,7 +60,7 @@ module Gitlab
expose
:closed
,
:updated_at
,
:created_at
end
class
Key
<
Grape
::
Entity
class
SSH
Key
<
Grape
::
Entity
expose
:id
,
:title
,
:key
end
end
...
...
lib/api/keys.rb
deleted
100644 → 0
View file @
4a072be2
module
Gitlab
# Keys API
class
Keys
<
Grape
::
API
before
{
authenticate!
}
resource
:keys
do
# Get currently authenticated user's keys
#
# Example Request:
# GET /keys
get
do
present
current_user
.
keys
,
with:
Entities
::
Key
end
# Get single key owned by currently authenticated user
#
# Example Request:
# GET /keys/:id
get
"/:id"
do
key
=
current_user
.
keys
.
find
params
[
:id
]
present
key
,
with:
Entities
::
Key
end
# Add new ssh key to currently authenticated user
#
# Parameters:
# key (required) - New SSH Key
# title (required) - New SSH Key's title
# Example Request:
# POST /keys
post
do
attrs
=
attributes_for_keys
[
:title
,
:key
]
key
=
current_user
.
keys
.
new
attrs
if
key
.
save
present
key
,
with:
Entities
::
Key
else
not_found!
end
end
# Delete existed ssh key of currently authenticated user
#
# Parameters:
# id (required) - SSH Key ID
# Example Request:
# DELETE /keys/:id
delete
"/:id"
do
key
=
current_user
.
keys
.
find
params
[
:id
]
key
.
delete
end
end
end
end
lib/api/users.rb
View file @
b6244581
...
...
@@ -25,12 +25,59 @@ module Gitlab
end
end
resource
:user
do
# Get currently authenticated user
#
# Example Request:
# GET /user
get
"/user"
do
get
do
present
@current_user
,
with:
Entities
::
User
end
# Get currently authenticated user's keys
#
# Example Request:
# GET /user/keys
get
"keys"
do
present
current_user
.
keys
,
with:
Entities
::
SSHKey
end
# Get single key owned by currently authenticated user
#
# Example Request:
# GET /user/keys/:id
get
"keys/:id"
do
key
=
current_user
.
keys
.
find
params
[
:id
]
present
key
,
with:
Entities
::
SSHKey
end
# Add new ssh key to currently authenticated user
#
# Parameters:
# key (required) - New SSH Key
# title (required) - New SSH Key's title
# Example Request:
# POST /user/keys
post
"keys"
do
attrs
=
attributes_for_keys
[
:title
,
:key
]
key
=
current_user
.
keys
.
new
attrs
if
key
.
save
present
key
,
with:
Entities
::
SSHKey
else
not_found!
end
end
# Delete existed ssh key of currently authenticated user
#
# Parameters:
# id (required) - SSH Key ID
# Example Request:
# DELETE /user/keys/:id
delete
"keys/:id"
do
key
=
current_user
.
keys
.
find
params
[
:id
]
key
.
delete
end
end
end
end
spec/requests/api/ssh_keys_spec.rb
deleted
100644 → 0
View file @
4a072be2
require
'spec_helper'
describe
Gitlab
::
Keys
do
include
ApiHelpers
let
(
:user
)
{
user
=
Factory
.
create
:user
user
.
reset_authentication_token!
user
}
let
(
:key
)
{
Factory
.
create
:key
,
{
user:
user
}}
describe
"GET /keys"
do
context
"when unauthenticated"
do
it
"should return authentication error"
do
get
api
(
"/keys"
)
response
.
status
.
should
==
401
end
end
context
"when authenticated"
do
it
"should return array of ssh keys"
do
user
.
keys
<<
key
user
.
save
get
api
(
"/keys"
,
user
)
response
.
status
.
should
==
200
json_response
.
should
be_an
Array
json_response
.
first
[
"title"
].
should
==
key
.
title
end
end
end
describe
"GET /keys/:id"
do
it
"should returm single key"
do
user
.
keys
<<
key
user
.
save
get
api
(
"/keys/
#{
key
.
id
}
"
,
user
)
response
.
status
.
should
==
200
json_response
[
"title"
].
should
==
key
.
title
end
it
"should return 404 Not Found within invalid ID"
do
get
api
(
"/keys/42"
,
user
)
response
.
status
.
should
==
404
end
end
describe
"POST /keys"
do
it
"should not create invalid ssh key"
do
post
api
(
"/keys"
,
user
),
{
title:
"invalid key"
}
response
.
status
.
should
==
404
end
it
"should create ssh key"
do
key_attrs
=
Factory
.
attributes
:key
expect
{
post
api
(
"/keys"
,
user
),
key_attrs
}.
to
change
{
user
.
keys
.
count
}.
by
(
1
)
end
end
describe
"DELETE /keys/:id"
do
it
"should delete existed key"
do
user
.
keys
<<
key
user
.
save
expect
{
delete
api
(
"/keys/
#{
key
.
id
}
"
,
user
)
}.
to
change
{
user
.
keys
.
count
}.
by
(
-
1
)
end
it
"should return 404 Not Found within invalid ID"
do
delete
api
(
"/keys/42"
,
user
)
response
.
status
.
should
==
404
end
end
end
spec/requests/api/users_spec.rb
View file @
b6244581
...
...
@@ -4,6 +4,7 @@ describe Gitlab::API do
include
ApiHelpers
let
(
:user
)
{
Factory
:user
}
let
(
:key
)
{
Factory
:key
,
user:
user
}
describe
"GET /users"
do
context
"when unauthenticated"
do
...
...
@@ -38,4 +39,64 @@ describe Gitlab::API do
json_response
[
'email'
].
should
==
user
.
email
end
end
describe
"GET /user/keys"
do
context
"when unauthenticated"
do
it
"should return authentication error"
do
get
api
(
"/user/keys"
)
response
.
status
.
should
==
401
end
end
context
"when authenticated"
do
it
"should return array of ssh keys"
do
user
.
keys
<<
key
user
.
save
get
api
(
"/user/keys"
,
user
)
response
.
status
.
should
==
200
json_response
.
should
be_an
Array
json_response
.
first
[
"title"
].
should
==
key
.
title
end
end
end
describe
"GET /user/keys/:id"
do
it
"should returm single key"
do
user
.
keys
<<
key
user
.
save
get
api
(
"/user/keys/
#{
key
.
id
}
"
,
user
)
response
.
status
.
should
==
200
json_response
[
"title"
].
should
==
key
.
title
end
it
"should return 404 Not Found within invalid ID"
do
get
api
(
"/user/keys/42"
,
user
)
response
.
status
.
should
==
404
end
end
describe
"POST /user/keys"
do
it
"should not create invalid ssh key"
do
post
api
(
"/user/keys"
,
user
),
{
title:
"invalid key"
}
response
.
status
.
should
==
404
end
it
"should create ssh key"
do
key_attrs
=
Factory
.
attributes
:key
expect
{
post
api
(
"/user/keys"
,
user
),
key_attrs
}.
to
change
{
user
.
keys
.
count
}.
by
(
1
)
end
end
describe
"DELETE /user/keys/:id"
do
it
"should delete existed key"
do
user
.
keys
<<
key
user
.
save
expect
{
delete
api
(
"/user/keys/
#{
key
.
id
}
"
,
user
)
}.
to
change
{
user
.
keys
.
count
}.
by
(
-
1
)
end
it
"should return 404 Not Found within invalid ID"
do
delete
api
(
"/user/keys/42"
,
user
)
response
.
status
.
should
==
404
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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