Skip to content

Commit

Permalink
Print SAML authentication URL for user convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
Rainer-Keller committed Oct 10, 2024
1 parent 429a9fd commit e4f5126
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* @param[out] dest the buffer to write the URL-encoded string
* @param[in] str the input string to be escaped
*/
static void url_encode(char *dest, const char *str)
void url_encode(char *dest, const char *str)
{
while (*str != '\0') {
if (isalnum(*str) || *str == '-' || *str == '_' ||
Expand Down
10 changes: 10 additions & 0 deletions src/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
#define ERR_HTTP_PERMISSION -6
#define ERR_HTTP_NO_COOKIE -7

/*
* URL-encodes a string for HTTP requests.
*
* The dest buffer size MUST be at least strlen(str) * 3 + 1.
*
* @param[out] dest the buffer to write the URL-encoded string
* @param[in] str the input string to be escaped
*/
void url_encode(char *dest, const char *str);

static inline const char *err_http_str(int code)
{
if (code > 0)
Expand Down
25 changes: 25 additions & 0 deletions src/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@
#include "config.h"
#include "log.h"
#include "tunnel.h"
#include "http.h" // for url_encode

static void print_url(const struct vpn_config *cfg) {
char *encoded_realm = NULL;
char realm[] = "&realm=";
char *empty_string = "\0";

// Desired string is https://company.com:port/remote/saml/start?redirect=1(&realm=<str>)
// with the realm being optional
static const char *uri_pattern = "https://%s:%d/remote/saml/start?redirect=1%s%s";

if (cfg->realm[0] != '\0') {
encoded_realm = alloca(strlen(cfg->realm) * 3 + 1); // url_encode required three times the size
url_encode(encoded_realm, cfg->realm);
} else {
encoded_realm = empty_string;
realm[0] = 0; // Make realm appear empty when printing as string
}

int required_size = 1 + snprintf(NULL, 0, uri_pattern, cfg->gateway_host, cfg->gateway_port, realm, encoded_realm);
char *url = alloca(required_size);
snprintf(url, required_size, uri_pattern, cfg->gateway_host, cfg->gateway_port, realm, encoded_realm);

log_info("Authenticate at '%s'\n", url);
}

// Convenience function to send a response with a user readable status message and the
// request URL shown for debug purposes.
Expand Down

0 comments on commit e4f5126

Please sign in to comment.