Skip to content

Commit d83bb40

Browse files
author
root
committed
fixed return_url
1 parent 1ec288e commit d83bb40

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/ngx_http_auth_jwt_module.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
8888
ngx_str_t passportKeyCookieName = ngx_string("PassportKey");
8989
ngx_str_t jwtCookieVal;
9090
char* jwtCookieValChrPtr;
91+
char* return_url;
9192
ngx_http_auth_jwt_loc_conf_t *jwtcf;
9293
u_char *keyBinary;
9394
jwt_t *jwt;
@@ -174,9 +175,40 @@ static ngx_int_t ngx_http_auth_jwt_handler(ngx_http_request_t *r)
174175
r->headers_out.___location->hash = 1;
175176
r->headers_out.___location->key.len = sizeof("Location") - 1;
176177
r->headers_out.___location->key.data = (u_char *) "Location";
177-
r->headers_out.___location->value.len = jwtcf->auth_jwt_loginurl.len;
178-
r->headers_out.___location->value.data = jwtcf->auth_jwt_loginurl.data;
179-
return NGX_HTTP_MOVED_PERMANENTLY;
178+
179+
if (r->method == NGX_HTTP_GET)
180+
{
181+
int loginlen = jwtcf->auth_jwt_loginurl.len;
182+
183+
char *scheme = (r->connection->ssl) ? "https" : "http";
184+
ngx_str_t server = r->headers_in.server;
185+
ngx_str_t uri = r->uri;
186+
187+
r->headers_out.___location->value.len = loginlen + sizeof("?return_url=") - 1 + strlen(scheme) + sizeof("://") - 1 + server.len + uri.len;
188+
return_url = ngx_alloc(r->headers_out.___location->value.len, r->connection->log);
189+
ngx_memcpy(return_url, jwtcf->auth_jwt_loginurl.data, jwtcf->auth_jwt_loginurl.len);
190+
int return_url_idx = jwtcf->auth_jwt_loginurl.len;
191+
ngx_memcpy(return_url+return_url_idx, "?return_url=", sizeof("?return_url=") - 1);
192+
return_url_idx += sizeof("?return_url=") - 1;
193+
ngx_memcpy(return_url+return_url_idx, scheme, strlen(scheme));
194+
return_url_idx += strlen(scheme);
195+
ngx_memcpy(return_url+return_url_idx, "://", sizeof("://") - 1);
196+
return_url_idx += sizeof("://") - 1;
197+
ngx_memcpy(return_url+return_url_idx, server.data, server.len);
198+
return_url_idx += server.len;
199+
ngx_memcpy(return_url+return_url_idx, uri.data, uri.len);
200+
return_url_idx += uri.len;
201+
r->headers_out.___location->value.data = (u_char *)return_url;
202+
203+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "redirect for get request");
204+
}
205+
else
206+
{
207+
r->headers_out.___location->value.len = jwtcf->auth_jwt_loginurl.len;
208+
r->headers_out.___location->value.data = jwtcf->auth_jwt_loginurl.data;
209+
}
210+
211+
return NGX_HTTP_MOVED_TEMPORARILY;
180212
}
181213

182214

@@ -276,3 +308,4 @@ hex_to_binary( const char* str, u_char* buf, int len ) {
276308
}
277309
return 0;
278310
}
311+

0 commit comments

Comments
 (0)