Skip to content

customizing your authentication by using nginx with nginx link function

Taymindis Woon edited this page Nov 18, 2018 · 1 revision
--- config
ngx_link_func_lib "/home/taymindis/github/nginx-link-function/t/libcfuntest.so";
location /backend {
    return 200 "Welcome ${arg_userName}";
}
location = /auth {
    internal;
    ngx_link_func_call "my_simple_authentication";
}
location = /my_simple_authentication {
  ngx_link_func_add_req_header userId $arg_userId;
  ngx_link_func_add_req_header userPass $arg_userPass;
  auth_request /auth;
  proxy_pass http://127.0.0.1:${server_port}/backend?userName=$http_userName;
}
void my_simple_authentication(ngx_link_func_ctx_t *ctx) {

    ngx_link_func_log_info(ctx, "Authenticating");
    char *userId = (char*) ngx_link_func_get_header(ctx, "userId");
    char *userPass = (char*) ngx_link_func_get_header(ctx, "userPass");
    char* userName;

    if ( userId == NULL || strlen(userId) == 0) {
AUTH_FAILED:
        ngx_link_func_write_resp(
            ctx,
            403,
            "403 Authenthication Failed",
            "text/plain",
            "",
            0
        );
    } else {
        userName = login(userId, userPass);
        /** Add input header for downstream response **/
        if (userName) {
            ngx_link_func_add_header_in(ctx, "userName", sizeof("userName")-1, userName, strlen(userName));
        } else {
            goto AUTH_FAILED;
        }

        ngx_link_func_write_resp(
            ctx,
            200,
            "200 OK",
            "text/plain",
            "OK",
            sizeof("OK")-1
        );
    }
}
curl http://127.0.0.1:8080/my_simple_authentication?userId=foo&userPass=xxxx

Full Docs and references API Documents