Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4133f8df22d5a59ff66b689d3641f4af26e1c446 (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.github.core.service;

import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_AUTHORIZATIONS;

import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.egit.github.core.Authorization;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.client.GitHubRequest;
import org.eclipse.egit.github.core.client.PagedRequest;

/**
 * Service for interacting with a user's OAUth authorizations
 *
 * @see <a href="http://developer.github.com/v3/oauth/">GitHub OAuth API
 *      documentation</a>
 */
public class OAuthService extends GitHubService {

	/**
	 * Create OAuth service
	 */
	public OAuthService() {
		super();
	}

	/**
	 * Create OAuth service
	 *
	 * @param client
	 */
	public OAuthService(GitHubClient client) {
		super(client);
	}

	/**
	 * Get all authorizations for currently authenticated user
	 *
	 * @return list of authorizations
	 * @throws IOException
	 */
	public List<Authorization> getAuthorizations() throws IOException {
		PagedRequest<Authorization> request = createPagedRequest();
		request.setUri(SEGMENT_AUTHORIZATIONS);
		request.setType(new TypeToken<List<Authorization>>() {
		}.getType());
		return getAll(request);
	}

	/**
	 * Get authorization with given id
	 *
	 * @param id
	 * @return authorization
	 * @throws IOException
	 */
	public Authorization getAuthorization(int id) throws IOException {
		GitHubRequest request = createRequest();
		StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
		uri.append('/').append(id);
		request.setUri(uri);
		request.setType(Authorization.class);
		return (Authorization) client.get(request).getBody();
	}

	/**
	 * Delete authorization with given id
	 *
	 * @param id
	 * @throws IOException
	 */
	public void deleteAuthorization(int id) throws IOException {
		StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
		uri.append('/').append(id);
		client.delete(uri.toString());
	}

	/**
	 * Create authorization
	 *
	 * @param authorization
	 * @return authorization
	 * @throws IOException
	 */
	public Authorization createAuthorization(Authorization authorization)
			throws IOException {
		return client.post(SEGMENT_AUTHORIZATIONS, authorization,
				Authorization.class);
	}

	/**
	 * Add scopes to authorization
	 *
	 * @param id
	 * @param scopes
	 * @return authorization
	 * @throws IOException
	 */
	public Authorization addScopes(int id, Collection<String> scopes)
			throws IOException {
		StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
		uri.append('/').append(id);
		Map<String, Collection<String>> params = Collections.singletonMap(
				"add_scopes", scopes); //$NON-NLS-1$
		return client.post(uri.toString(), params, Authorization.class);
	}

	/**
	 * Remove scopes from authorization
	 *
	 * @param id
	 * @param scopes
	 * @return authorization
	 * @throws IOException
	 */
	public Authorization removeScopes(int id, Collection<String> scopes)
			throws IOException {
		StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
		uri.append('/').append(id);
		Map<String, Collection<String>> params = Collections.singletonMap(
				"remove_scopes", scopes); //$NON-NLS-1$
		return client.post(uri.toString(), params, Authorization.class);
	}

	/**
	 * Set scopes for authorization
	 *
	 * @param id
	 * @param scopes
	 * @return authorization
	 * @throws IOException
	 */
	public Authorization setScopes(int id, Collection<String> scopes)
			throws IOException {
		StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
		uri.append('/').append(id);
		Map<String, Collection<String>> params = Collections.singletonMap(
				"scopes", scopes); //$NON-NLS-1$
		return client.post(uri.toString(), params, Authorization.class);
	}
}

Back to the top