-
Notifications
You must be signed in to change notification settings - Fork 30
Introduce RBAC checking via custom authHandler function #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v1.x/staging
Are you sure you want to change the base?
Changes from 22 commits
756b42c
37a9bd7
d11786a
552f551
56d3941
faa29df
f6d670d
f8e6a20
1b55568
c5c9bac
20ad412
1673b5f
632cb11
517679b
305ab94
be35a36
166c715
be5f0e5
59fc275
c5880dd
16b4ef9
979e105
9d7f631
2a1ab84
e736e8d
2f45fb2
58bf017
14a7225
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3349,6 +3349,27 @@ static int handleServiceFailed(HttpConversation *conversation, | |
return HTTP_SERVICE_FAILED; | ||
} | ||
|
||
static int checkAuthorization(HttpServer *server, HttpService *service, HttpRequest *request, HttpResponse *response) { | ||
if (!request->authenticated) { | ||
return FALSE; | ||
} | ||
if (service->authorizationType == SERVICE_AUTHORIZATION_TYPE_NONE) { | ||
return TRUE; | ||
} | ||
int authorized = TRUE; | ||
HttpAuthorizationHandler *handler = server->authorizationHandlerList; | ||
while (handler) { | ||
if (handler->authorizationType == service->authorizationType) { | ||
authorized = handler->authorizationHandler(service, request, response, handler->userData); | ||
if (!authorized) { | ||
break; | ||
} | ||
} | ||
handler = handler->next; | ||
} | ||
return authorized; | ||
} | ||
|
||
static int handleHttpService(HttpServer *server, | ||
HttpService *service, | ||
HttpRequest *request, | ||
|
@@ -3435,8 +3456,9 @@ static int handleHttpService(HttpServer *server, | |
} | ||
break; | ||
} | ||
int authorized = checkAuthorization(server, service, request, response); | ||
#ifdef DEBUG | ||
printf("service=%s authenticated=%d\n",service->name,request->authenticated); | ||
printf("service=%s authenticated=%d authorized=%d\n",service->name,request->authenticated,authorized); | ||
#endif | ||
if (request->authenticated == FALSE){ | ||
if (service->authFlags & SERVICE_AUTH_FLAG_OPTIONAL) { | ||
|
@@ -3445,6 +3467,8 @@ static int handleHttpService(HttpServer *server, | |
} else { | ||
respondWithAuthError(response, &authResponse); | ||
} | ||
} else if (!authorized) { | ||
respondWithError(response, HTTP_STATUS_FORBIDDEN, "Forbidden"); | ||
// Response is finished on return | ||
} else { | ||
|
||
|
@@ -6001,7 +6025,27 @@ int mainHttpLoop(HttpServer *server){ | |
return stcBaseMainLoop(base, MAIN_WAIT_MILLIS); | ||
} | ||
|
||
|
||
void registerHttpAuthorizationHandler(HttpServer *server, int authorizationType, AuthorizationHandler *authorizationHandler, void *userData) { | ||
ifakhrutdinov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (authorizationType == SERVICE_AUTHORIZATION_TYPE_NONE) { | ||
return; | ||
} | ||
HttpAuthorizationHandler *handler = (HttpAuthorizationHandler*) safeMalloc(sizeof(*handler), "HttpAuthorizationHandler"); | ||
if (handler) { | ||
handler->authorizationType = authorizationType; | ||
handler->authorizationHandler = authorizationHandler; | ||
handler->userData = userData; | ||
handler->next = NULL; | ||
HttpAuthorizationHandler *head = server->authorizationHandlerList; | ||
if (!head) { | ||
server->authorizationHandlerList = handler; | ||
} else { | ||
while (head->next != NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A nit: you could've used first/last pointers to do O(1) insertion: if (server->firstAuthHandler) {
server->lastAuthHandler->next = handler;
} else {
server->firstAuthHandler = handler;
}
server->lastAuthHandler = handler; Or if you don't care about the current order: handler->next = server->authorizationHandlerList;
server->authorizationHandlerList = handler; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation not so critical for now as we have only one authorization handler. It can be improved in the future when we for sure know what we want to optimize. |
||
head = head->next; | ||
} | ||
head->next = handler; | ||
} | ||
} | ||
} | ||
|
||
|
||
/* | ||
|
Uh oh!
There was an error while loading. Please reload this page.