Skip to content

Commit ad08a49

Browse files
committed
Authentication Code.
1 parent 02735b7 commit ad08a49

25 files changed

+2189
-100
lines changed

core/src/main/java/com/github/api/v2/services/GitHubAuthenticator.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,8 @@ public interface GitHubAuthenticator extends GitHubCommunicator {
1414
*
1515
* @param applicationKey the new application key
1616
*/
17-
public void setApplicationKey(String applicationKey);
17+
public void setCredentials(String userName, String applicationKey);
1818

19-
/**
20-
* Gets the application key.
21-
*
22-
* @return the application key
23-
*/
24-
public String getApplicationKey();
25-
2619
/**
2720
* Sets the user ip address.
2821
*

core/src/main/java/com/github/api/v2/services/GitHubServiceFactory.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@
1919
*/
2020
public class GitHubServiceFactory {
2121

22-
/** The application key. */
23-
private String applicationKey;
24-
2522
/**
2623
* Instantiates a new google search query factory.
2724
*
2825
* @param applicationKey the application key
2926
*/
30-
private GitHubServiceFactory(String applicationKey) {
31-
this.applicationKey = applicationKey;
27+
private GitHubServiceFactory() {
3228
}
3329

3430
/**
@@ -38,8 +34,8 @@ private GitHubServiceFactory(String applicationKey) {
3834
*
3935
* @return the google search query factory
4036
*/
41-
public static GitHubServiceFactory newInstance(String applicationKey) {
42-
return new GitHubServiceFactory(applicationKey);
37+
public static GitHubServiceFactory newInstance() {
38+
return new GitHubServiceFactory();
4339
}
4440

4541
/**
@@ -48,7 +44,7 @@ public static GitHubServiceFactory newInstance(String applicationKey) {
4844
* @return the blog search query
4945
*/
5046
public CommitService createCommitService() {
51-
return new CommitServiceImpl(applicationKey);
47+
return new CommitServiceImpl();
5248
}
5349

5450
/**
@@ -57,7 +53,7 @@ public CommitService createCommitService() {
5753
* @return the book search query
5854
*/
5955
public GistService createGistService() {
60-
return new GistServiceImpl(applicationKey);
56+
return new GistServiceImpl();
6157
}
6258

6359
/**
@@ -66,7 +62,7 @@ public GistService createGistService() {
6662
* @return the image search query
6763
*/
6864
public IssueService createIssueService() {
69-
return new IssueServiceImpl(applicationKey);
65+
return new IssueServiceImpl();
7066
}
7167

7268
/**
@@ -75,7 +71,7 @@ public IssueService createIssueService() {
7571
* @return the local search query
7672
*/
7773
public NetworkService createNetworkService() {
78-
return new NetworkServiceImpl(applicationKey);
74+
return new NetworkServiceImpl();
7975
}
8076

8177
/**
@@ -84,7 +80,7 @@ public NetworkService createNetworkService() {
8480
* @return the news search query
8581
*/
8682
public ObjectService createObjectService() {
87-
return new ObjectServiceImpl(applicationKey);
83+
return new ObjectServiceImpl();
8884
}
8985

9086
/**
@@ -93,7 +89,7 @@ public ObjectService createObjectService() {
9389
* @return the patent search query
9490
*/
9591
public RepositoryService createRepositoryService() {
96-
return new RepositoryServiceImpl(applicationKey);
92+
return new RepositoryServiceImpl();
9793
}
9894

9995
/**
@@ -102,6 +98,6 @@ public RepositoryService createRepositoryService() {
10298
* @return the video search query
10399
*/
104100
public UserService createUserService() {
105-
return new UserServiceImpl(applicationKey);
101+
return new UserServiceImpl();
106102
}
107103
}

core/src/main/java/com/github/api/v2/services/impl/BaseGitHubService.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.lang.reflect.Type;
66
import java.util.ArrayList;
77
import java.util.Collections;
8-
import java.util.HashMap;
98
import java.util.List;
109
import java.util.Map;
1110

@@ -49,10 +48,7 @@ public abstract class BaseGitHubService extends GitHubApiGateway implements GitH
4948
*
5049
* @param applicationId the application id
5150
*/
52-
public BaseGitHubService(String applicationId) {
53-
super.setApplicationKey(applicationId);
54-
requestHeaders = new HashMap<String, String>();
55-
51+
public BaseGitHubService() {
5652
// by default we compress contents
5753
requestHeaders.put("Accept-Encoding", "gzip, deflate");
5854
}
@@ -63,9 +59,8 @@ public BaseGitHubService(String applicationId) {
6359
* @param applicationId the application id
6460
* @param apiVersion the api version
6561
*/
66-
public BaseGitHubService(String applicationId, String apiVersion) {
67-
this(applicationId);
68-
super.setApiVersion(apiVersion);
62+
public BaseGitHubService(String apiVersion) {
63+
setApiVersion(apiVersion);
6964
}
7065

7166
/**

core/src/main/java/com/github/api/v2/services/impl/CommitServiceImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
public class CommitServiceImpl extends BaseGitHubService implements
2121
CommitService {
2222

23-
public CommitServiceImpl(String applicationId) {
24-
super(applicationId);
25-
}
26-
2723
@Override
2824
public Commit getCommit(String userName, String repositoryName, String sha) {
2925
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.CommitApiUrls.GET_COMMIT_URL);

core/src/main/java/com/github/api/v2/services/impl/GistServiceImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
public class GistServiceImpl extends BaseGitHubService implements
2222
GistService {
2323

24-
public GistServiceImpl(String applicationId) {
25-
super(applicationId);
26-
}
27-
2824
@Override
2925
public Gist getGist(String gistId) {
3026
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.GistApiUrls.GET_GIST_URL);

core/src/main/java/com/github/api/v2/services/impl/GitHubApiGateway.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import java.net.HttpURLConnection;
1515
import java.net.URL;
1616
import java.net.URLEncoder;
17+
import java.util.HashMap;
1718
import java.util.Map;
1819
import java.util.logging.Level;
1920
import java.util.logging.Logger;
2021
import java.util.zip.GZIPInputStream;
2122

2223
import com.github.api.v2.services.GitHubException;
2324
import com.github.api.v2.services.constant.ApplicationConstants;
25+
import com.github.api.v2.services.util.Base64;
2426

2527
/**
2628
* The Class GoogleSearchApiGateway.
@@ -36,11 +38,14 @@ public abstract class GitHubApiGateway {
3638
/** The Constant REFERRER. */
3739
private static final String REFERRER = "Referer";
3840

39-
/** The request headers. */
40-
protected Map<String, String> requestHeaders;
41+
/** The Constant REFERRER. */
42+
private static final String AUTHORIZATION = "Authorization";
4143

42-
/** The application key. */
43-
protected String applicationKey;
44+
/** The Constant REFERRER. */
45+
private static final String BASIC = "Basic ";
46+
47+
/** The request headers. */
48+
protected Map<String, String> requestHeaders = new HashMap<String, String>();
4449

4550
/** The user ip address. */
4651
protected String userIpAddress;
@@ -103,24 +108,6 @@ public void removeRequestHeader(String headerName) {
103108
requestHeaders.remove(headerName);
104109
}
105110

106-
/**
107-
* Gets the application key.
108-
*
109-
* @return the application key
110-
*/
111-
public String getApplicationKey() {
112-
return applicationKey;
113-
}
114-
115-
/**
116-
* Sets the application key.
117-
*
118-
* @param applicationKey the new application key
119-
*/
120-
public void setApplicationKey(String applicationKey) {
121-
this.applicationKey = applicationKey;
122-
}
123-
124111
/**
125112
* Sets the referrer.
126113
*
@@ -138,6 +125,16 @@ public void setReferrer(String referrer) {
138125
public void setUserIpAddress(String userIpAddress) {
139126
this.userIpAddress = userIpAddress;
140127
}
128+
129+
/**
130+
* Sets the application key.
131+
*
132+
* @param applicationKey the new application key
133+
*/
134+
public void setCredentials(String userName, String applicationKey) {
135+
String credentials = userName + ":" + applicationKey;
136+
requestHeaders.put(AUTHORIZATION, BASIC + Base64.encodeBytes(credentials.getBytes()));
137+
}
141138

142139
/**
143140
* Convert stream to string.

core/src/main/java/com/github/api/v2/services/impl/IssueServiceImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
public class IssueServiceImpl extends BaseGitHubService implements
2525
IssueService {
2626

27-
public IssueServiceImpl(String applicationId) {
28-
super(applicationId);
29-
}
30-
3127
@Override
3228
public void addComment(String userName, String repositoryName,
3329
int issueNumber, String comment) {

core/src/main/java/com/github/api/v2/services/impl/NetworkServiceImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
public class NetworkServiceImpl extends BaseGitHubService implements
2222
NetworkService {
2323

24-
public NetworkServiceImpl(String applicationId) {
25-
super(applicationId);
26-
}
27-
2824
@Override
2925
public List<Commit> getNetworkData(String userName, String repositoryName,
3026
String networkHash) {

core/src/main/java/com/github/api/v2/services/impl/ObjectServiceImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
public class ObjectServiceImpl extends BaseGitHubService implements
2323
ObjectService {
2424

25-
public ObjectServiceImpl(String applicationId) {
26-
super(applicationId);
27-
}
28-
2925
@Override
3026
public Blob getBlob(String userName, String repositoryName, String treeSha,
3127
String filePath) {

core/src/main/java/com/github/api/v2/services/impl/RepositoryServiceImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
public class RepositoryServiceImpl extends BaseGitHubService implements
2828
RepositoryService {
2929

30-
public RepositoryServiceImpl(String applicationId) {
31-
super(applicationId);
32-
}
33-
3430
@Override
3531
public void addCollaborator(String repositoryName, String collaboratorName) {
3632
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.RepositoryApiUrls.ADD_COLLABORATOR_URL);

0 commit comments

Comments
 (0)