Skip to content

Commit c9cd1b1

Browse files
author
Leonty Chudinov
committed
Refactor HTTP Server Authentication code
Signed-off-by: Leonty Chudinov <[email protected]>
1 parent ef72720 commit c9cd1b1

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

c/httpserver.c

+20-7
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ typedef struct AuthResponse_tag {
9292
union {
9393
SAFAuthStatus safStatus;
9494
} responseDetails;
95+
bool authDataFound;
9596
} AuthResponse;
9697

9798
/* FIX THIS: a temporary "low profile" way of hiding printfs. Improves
@@ -2485,6 +2486,7 @@ static int safAuthenticate(HttpService *service, HttpRequest *request, AuthRespo
24852486

24862487
authResponse->type = AUTH_TYPE_RACF;
24872488
authResponse->responseDetails.safStatus = status.safStatus;
2489+
authResponse->authDataFound = (bool)authDataFound;
24882490

24892491
if (pwdCheckRC != 0) {
24902492
#ifdef DEBUG_AUTH
@@ -2868,7 +2870,7 @@ static int serviceAuthNativeWithSessionToken(HttpService *service, HttpRequest *
28682870
response->sessionCookie = NULL;
28692871

28702872
AUTH_TRACE("AUTH: tokenCookieText: %s\n",(tokenCookieText ? tokenCookieText : "<noAuthToken>"));
2871-
2873+
authResponse->authDataFound = authDataFound || (tokenCookieText != NULL);
28722874
if (tokenCookieText){
28732875
zowelog(NULL, LOG_COMP_HTTPSERVER, ZOWE_LOG_DEBUG3,
28742876
"serviceAuthNativeWithSessionToken: tokenCookieText: %s\n",
@@ -2933,7 +2935,8 @@ static int serviceAuthNativeWithSessionToken(HttpService *service, HttpRequest *
29332935

29342936
static int serviceAuthWithJwt(HttpService *service,
29352937
HttpRequest *request,
2936-
HttpResponse *response) {
2938+
HttpResponse *response,
2939+
AuthResponse *authResponse) {
29372940
HttpHeader *const authorizationHeader = getHeader(request, "Authorization");
29382941
char *jwtTokenText = getCookieValue(request,JWT_COOKIE_NAME);
29392942

@@ -2971,6 +2974,7 @@ static int serviceAuthWithJwt(HttpService *service,
29712974
if (request->authToken == NULL) {
29722975
return FALSE;
29732976
}
2977+
authResponse->authDataFound = TRUE;
29742978

29752979
JwtContext *const jwtContext = service->server->config->jwtContext;
29762980
if (jwtContext == NULL) {
@@ -3232,12 +3236,12 @@ static int handleHttpService(HttpServer *server,
32323236

32333237
int clearSessionToken = FALSE;
32343238

3235-
AuthResponse authResponse;
3239+
AuthResponse authResponse = {0};
32363240

32373241
switch (service->authType){
32383242

32393243
case SERVICE_AUTH_NONE:
3240-
request->authenticated = TRUE;
3244+
request->authenticated = FALSE;
32413245
break;
32423246
case SERVICE_AUTH_SAF:
32433247
/* SAF Authentication just checks that user is known at ALL to SAF.
@@ -3259,7 +3263,7 @@ static int handleHttpService(HttpServer *server,
32593263
switch (server->config->authTokenType) {
32603264
case SERVICE_AUTH_TOKEN_TYPE_JWT:
32613265
case SERVICE_AUTH_TOKEN_TYPE_JWT_WITH_LEGACY_FALLBACK:
3262-
request->authenticated = serviceAuthWithJwt(service, request, response);
3266+
request->authenticated = serviceAuthWithJwt(service, request, response, &authResponse);
32633267

32643268
if (request->authenticated ||
32653269
service->server->config->authTokenType
@@ -3275,9 +3279,18 @@ static int handleHttpService(HttpServer *server,
32753279
#ifdef DEBUG
32763280
printf("service=%s authenticated=%d\n",service->name,request->authenticated);
32773281
#endif
3282+
bool isAuthOptional = service->authFlags & SERVICE_AUTH_FLAG_OPTIONAL;
3283+
bool allowUnauthenticated = (service->authType == SERVICE_AUTH_NONE) ||
3284+
(isAuthOptional && !authResponse.authDataFound);
3285+
AUTH_TRACE("authType %d, authenticated %s, authFlagOptional %s, authDataFound %s, allowUnauthenticated %s\n",
3286+
service->authType,
3287+
request->authenticated ? "true" : "false",
3288+
isAuthOptional ? "true" : "false",
3289+
authResponse.authDataFound ? "true" : "false",
3290+
allowUnauthenticated ? "true" : "false"
3291+
);
32783292
if (request->authenticated == FALSE){
3279-
if (service->authFlags & SERVICE_AUTH_FLAG_OPTIONAL) {
3280-
// Allow the service to decide when to respond with HTTP 401
3293+
if (allowUnauthenticated) {
32813294
serveRequest(service, response, request);
32823295
} else {
32833296
respondWithAuthError(response, &authResponse);

0 commit comments

Comments
 (0)