17
17
import jakarta .mail .internet .AddressException ;
18
18
import jakarta .mail .internet .InternetAddress ;
19
19
import java .io .IOException ;
20
- import java .io .InputStream ;
21
- import java .lang .reflect .Type ;
22
- import java .net .HttpURLConnection ;
23
- import java .net .URL ;
24
- import javax .inject .Inject ;
25
- import javax .inject .Named ;
26
20
import javax .inject .Singleton ;
27
21
import org .eclipse .che .api .auth .shared .dto .OAuthToken ;
28
- import org .eclipse .che .commons .annotation .Nullable ;
29
- import org .eclipse .che .commons .json .JsonHelper ;
30
- import org .eclipse .che .commons .json .JsonParseException ;
31
22
import org .eclipse .che .security .oauth .shared .User ;
32
23
33
24
/** OAuth authentication for github account. */
34
25
@ Singleton
35
26
public class GitHubOAuthAuthenticator extends OAuthAuthenticator {
36
- @ Inject
37
27
public GitHubOAuthAuthenticator (
38
- @ Nullable @ Named ("che.oauth.github.clientid" ) String clientId ,
39
- @ Nullable @ Named ("che.oauth.github.clientsecret" ) String clientSecret ,
40
- @ Nullable @ Named ("che.oauth.github.redirecturis" ) String [] redirectUris ,
41
- @ Nullable @ Named ("che.oauth.github.authuri" ) String authUri ,
42
- @ Nullable @ Named ("che.oauth.github.tokenuri" ) String tokenUri )
28
+ String clientId , String clientSecret , String [] redirectUris , String authUri , String tokenUri )
43
29
throws IOException {
44
- if (!isNullOrEmpty (clientId )
45
- && !isNullOrEmpty (clientSecret )
46
- && !isNullOrEmpty (authUri )
47
- && !isNullOrEmpty (tokenUri )
48
- && redirectUris != null
49
- && redirectUris .length != 0 ) {
50
-
51
- configure (
52
- clientId , clientSecret , redirectUris , authUri , tokenUri , new MemoryDataStoreFactory ());
53
- }
30
+ configure (
31
+ clientId , clientSecret , redirectUris , authUri , tokenUri , new MemoryDataStoreFactory ());
54
32
}
55
33
56
34
@ Override
57
35
public User getUser (OAuthToken accessToken ) throws OAuthAuthenticationException {
58
36
GitHubUser user =
59
- getJson (
60
- "https://api.github.com/user?access_token=" + accessToken .getToken (), GitHubUser .class );
61
-
62
- GithubEmail [] result =
63
- getJson2 (
64
- "https://api.github.com/user/emails?access_token=" + accessToken .getToken (),
65
- GithubEmail [].class ,
66
- null );
37
+ getJson ("https://api.github.com/user" , accessToken .getToken (), GitHubUser .class );
38
+ final String email = user .getEmail ();
67
39
68
- GithubEmail verifiedEmail = null ;
69
- for (GithubEmail email : result ) {
70
- if (email .isPrimary () && email .isVerified ()) {
71
- verifiedEmail = email ;
72
- break ;
73
- }
74
- }
75
- if (verifiedEmail == null
76
- || verifiedEmail .getEmail () == null
77
- || verifiedEmail .getEmail ().isEmpty ()) {
40
+ if (isNullOrEmpty (email )) {
78
41
throw new OAuthAuthenticationException (
79
42
"Sorry, we failed to find any verified emails associated with your GitHub account."
80
43
+ " Please, verify at least one email in your GitHub account and try to connect with GitHub again." );
81
44
}
82
- user .setEmail (verifiedEmail .getEmail ());
83
- final String email = user .getEmail ();
84
45
try {
85
46
new InternetAddress (email ).validate ();
86
47
} catch (AddressException e ) {
@@ -89,32 +50,6 @@ public User getUser(OAuthToken accessToken) throws OAuthAuthenticationException
89
50
return user ;
90
51
}
91
52
92
- protected <O > O getJson2 (String getUserUrl , Class <O > userClass , Type type )
93
- throws OAuthAuthenticationException {
94
- HttpURLConnection urlConnection = null ;
95
- InputStream urlInputStream = null ;
96
-
97
- try {
98
- urlConnection = (HttpURLConnection ) new URL (getUserUrl ).openConnection ();
99
- urlConnection .setRequestProperty ("Accept" , "application/vnd.github.v3.html+json" );
100
- urlInputStream = urlConnection .getInputStream ();
101
- return JsonHelper .fromJson (urlInputStream , userClass , type );
102
- } catch (JsonParseException | IOException e ) {
103
- throw new OAuthAuthenticationException (e .getMessage (), e );
104
- } finally {
105
- if (urlInputStream != null ) {
106
- try {
107
- urlInputStream .close ();
108
- } catch (IOException ignored ) {
109
- }
110
- }
111
-
112
- if (urlConnection != null ) {
113
- urlConnection .disconnect ();
114
- }
115
- }
116
- }
117
-
118
53
@ Override
119
54
public final String getOAuthProvider () {
120
55
return "github" ;
@@ -123,62 +58,19 @@ public final String getOAuthProvider() {
123
58
@ Override
124
59
public OAuthToken getToken (String userId ) throws IOException {
125
60
final OAuthToken token = super .getToken (userId );
126
- if (!(token == null || token .getToken () == null || token .getToken ().isEmpty ())) {
127
- // Need to check if token which stored is valid for requests, then if valid - we returns it to
128
- // caller
129
- String tokenVerifyUrl = "https://api.github.com/?access_token=" + token .getToken ();
130
- HttpURLConnection http = null ;
131
- try {
132
- http = (HttpURLConnection ) new URL (tokenVerifyUrl ).openConnection ();
133
- http .setInstanceFollowRedirects (false );
134
- http .setRequestMethod ("GET" );
135
- http .setRequestProperty ("Accept" , "application/json" );
136
-
137
- if (http .getResponseCode () == 401 ) {
138
- return null ;
139
- }
140
- } finally {
141
- if (http != null ) {
142
- http .disconnect ();
143
- }
61
+ // Need to check if token which is stored is valid for requests, then if valid - we returns it
62
+ // to
63
+ // caller
64
+ try {
65
+ if (token == null
66
+ || token .getToken () == null
67
+ || token .getToken ().isEmpty ()
68
+ || getJson ("https://api.github.com/user" , token .getToken (), GitHubUser .class ) == null ) {
69
+ return null ;
144
70
}
145
-
146
- return token ;
147
- }
148
- return null ;
149
- }
150
-
151
- /**
152
- * information for each email address indicating if the address has been verified and if it’s the
153
- * user’s primary email address for GitHub.
154
- */
155
- public static class GithubEmail {
156
- private boolean primary ;
157
- private boolean verified ;
158
- private String email ;
159
-
160
- public boolean isPrimary () {
161
- return primary ;
162
- }
163
-
164
- public void setPrimary (boolean primary ) {
165
- this .primary = primary ;
166
- }
167
-
168
- public boolean isVerified () {
169
- return verified ;
170
- }
171
-
172
- public void setVerified (boolean verified ) {
173
- this .verified = verified ;
174
- }
175
-
176
- public String getEmail () {
177
- return email ;
178
- }
179
-
180
- public void setEmail (String email ) {
181
- this .email = email ;
71
+ } catch (OAuthAuthenticationException e ) {
72
+ return null ;
182
73
}
74
+ return token ;
183
75
}
184
76
}
0 commit comments