15
15
*/
16
16
package com .yahoo .athenz .common .server .rest ;
17
17
18
+ import java .util .HashSet ;
18
19
import java .util .List ;
19
20
import java .util .ArrayList ;
20
21
import java .security .cert .X509Certificate ;
22
+ import java .util .Set ;
21
23
22
24
import javax .servlet .http .HttpServletRequest ;
23
25
@@ -33,9 +35,11 @@ public class Http {
33
35
34
36
private static final Logger LOG = LoggerFactory .getLogger (Http .class );
35
37
38
+ public static final String WWW_AUTHENTICATE = "WWW-Authenticate" ;
36
39
public static final String INVALID_CRED_ATTR = "com.yahoo.athenz.auth.credential.error" ;
40
+ public static final String AUTH_CHALLENGES = "com.yahoo.athenz.auth.credential.challenges" ;
37
41
public static final String JAVAX_CERT_ATTR = "javax.servlet.request.X509Certificate" ;
38
-
42
+
39
43
public static class AuthorityList {
40
44
List <Authority > authorities ;
41
45
@@ -66,7 +70,7 @@ static String getCookieValue(HttpServletRequest hreq, String name) {
66
70
return null ;
67
71
}
68
72
69
- private static String authenticatingCredentials (HttpServletRequest request ,
73
+ static String authenticatingCredentials (HttpServletRequest request ,
70
74
Authority authority ) {
71
75
final String header = authority .getHeader ();
72
76
if (header == null ) {
@@ -91,12 +95,14 @@ public static Principal authenticate(HttpServletRequest request,
91
95
}
92
96
93
97
StringBuilder authErrMsg = new StringBuilder (512 );
98
+ Set <String > authChallenges = null ;
94
99
for (Authority authority : authorities .authorities ) {
95
100
Principal principal = null ;
96
101
StringBuilder errMsg = new StringBuilder (512 );
97
102
switch (authority .getCredSource ()) {
98
103
case HEADER :
99
104
String creds = authenticatingCredentials (request , authority );
105
+
100
106
if (creds != null ) {
101
107
principal = authority .authenticate (creds , ServletRequestUtil .getRemoteAddress (request ),
102
108
request .getMethod (), errMsg );
@@ -119,7 +125,15 @@ public static Principal authenticate(HttpServletRequest request,
119
125
if (principal != null ) {
120
126
return principal ;
121
127
}
122
-
128
+
129
+ final String challenge = authority .getAuthenticateChallenge ();
130
+ if (challenge != null ) {
131
+ if (authChallenges == null ) {
132
+ authChallenges = new HashSet <>();
133
+ }
134
+ authChallenges .add (challenge );
135
+ }
136
+
123
137
// otherwise if we have a specific error message from an authority
124
138
// then we'll keep it in case all other authorities also fail and
125
139
// we need to log the reason for failure
@@ -151,6 +165,14 @@ public static Principal authenticate(HttpServletRequest request,
151
165
LOG .error ("authenticate: No credentials provided" );
152
166
}
153
167
168
+ // if we have challenges specified, we're going to set it as a request
169
+ // attribute and let the caller decide if they want to add it to the
170
+ // response as a header in its context handler
171
+
172
+ if (authChallenges != null ) {
173
+ request .setAttribute (AUTH_CHALLENGES , String .join (", " , authChallenges ));
174
+ }
175
+
154
176
throw new ResourceException (ResourceException .UNAUTHORIZED , "Invalid credentials" );
155
177
}
156
178
@@ -165,9 +187,6 @@ public static String authorizedUser(HttpServletRequest request,
165
187
String resource , String otherDomain ) {
166
188
Principal principal = authenticate (request , authorities );
167
189
authorize (authorizer , principal , action , resource , otherDomain );
168
- if (principal == null ) {
169
- return null ;
170
- }
171
190
return principal .getFullName ();
172
191
}
173
192
0 commit comments