// OAuth authorization server URL
*server_name = getenv("SERVER_NAME"),
// SERVER_NAME value
- *server_port = getenv("SERVER_PORT");
+ *server_port = getenv("SERVER_PORT"),
// SERVER_PORT value
+ *state = NULL; // State string
char *client_id = NULL, // Client ID value
*code_verifier = NULL, // Code verifier string
*nonce = NULL, // Nonce string
redirect_uri[1024], // redirect_uri value
- *state = NULL, // State string
*url = NULL; // Authorization URL
cups_json_t *metadata = NULL; // OAuth metadata
// Make state and code verification strings...
code_verifier = cupsOAuthMakeBase64Random(128);
nonce = cupsOAuthMakeBase64Random(16);
- state = cupsOAuthMakeBase64Random(16);
+ state = cgiGetCookie(CUPS_SID);
// Get the authorization URL
if ((url = cupsOAuthMakeAuthorizationURL(oauth_uri, metadata, /*resource_uri*/NULL, getenv("CUPS_OAUTH_SCOPES"), client_id, code_verifier, nonce, redirect_uri, state)) == NULL)
free(code_verifier);
cupsJSONDelete(metadata);
free(nonce);
- free(state);
free(url);
}
fprintf(stderr, "DEBUG2: do_redirect(url=\"%s\")\n", url);
if (url && (!strncmp(url, "http://", 7) || !strncmp(url, "https://", 8)))
- printf("Location: %s\n\n", url);
+ printf("Location: %s\n", url);
else
- printf("Location: %s://%s:%s%s\n\n", getenv("HTTPS") ? "https" : "http", getenv("SERVER_NAME"), getenv("SERVER_PORT"), url ? url : "/");
+ printf("Location: %s://%s:%s%s\n", getenv("HTTPS") ? "https" : "http", getenv("SERVER_NAME"), getenv("SERVER_PORT"), url ? url : "/");
puts("Content-Type: text/plain\n");
puts("Redirecting...");
+ fflush(stdout);
+
+
+ if (url && (!strncmp(url, "http://", 7) || !strncmp(url, "https://", 8)))
+ fprintf(stderr, "DEBUG2: do_redirect: Location: %s\n", url);
+ else
+ fprintf(stderr, "DEBUG2: do_redirect: Location: %s://%s:%s%s\n", getenv("HTTPS") ? "https" : "http", getenv("SERVER_NAME"), getenv("SERVER_PORT"), url ? url : "/");
+
+ fputs("DEBUG2: do_redirect: Content-Type: text/plain\n", stderr);
+ fputs("DEBUG2: do_redirect:\n", stderr);
+ fputs("DEBUG2: do_redirect: Redirecting...", stderr);
}
*client_id = NULL, // Client ID value
*error, // Error string
redirect_uri[1024]; // redirect_uri value
- const char *code, // Authorization code
- *state_cookie, // State cookie
- *state_var; // State variable
+ const char *code; // Authorization code
cups_json_t *metadata = NULL; // OAuth metadata
time_t access_expires; // When the bearer token expires
fprintf(stderr, "DEBUG2: finish_login: client_id=\"%s\"\n", client_id);
- // Get the state and code strings...
- code = cgiGetVariable("code");
- state_cookie = cgiGetCookie("CUPS_OAUTH_STATE");
- state_var = cgiGetVariable("state");
-
- if (!state_cookie || !state_var || strcmp(state_cookie, state_var))
- {
- show_error(cgiText(_("OAuth Login")), cgiText(_("Unable to authorize access")), cgiText(_("Bad client state value in response.")));
- goto done;
- }
+ // Get the code string...
+ code = cgiGetVariable("code");
// Get the access token...
if ((bearer = cupsOAuthGetTokens(oauth_uri, metadata, /*resource_uri*/NULL, code, CUPS_OGRANT_AUTHORIZATION_CODE, redirect_uri, &access_expires)) == NULL)
goto done;
}
+ fprintf(stderr, "DEBUG2: finish_login: access_token=\"%s\", access_expires=%ld\n", bearer, (long)access_expires);
+
// Save it as a cookie...
cgiSetCookie("CUPS_BEARER", bearer, /*path*/NULL, /*domain*/NULL, access_expires, /*secure*/0);
// Redirect...
do_redirect(cgiGetCookie("CUPS_REFERRER"));
+ fputs("DEBUG2: finish_login: After redirect.\n", stderr);
+
done:
// Free memory...
#include <cups/http.h>
-/*
- * Session ID name
- */
-
-#define CUPS_SID "org.cups.sid"
-
-
/*
* Data structure to hold all the CGI form variables and arrays...
*/
else if (!cgi_initialize_post())
return (0);
- if ((cups_sid_form = cgiGetVariable(CUPS_SID)) == NULL ||
- strcmp(cups_sid_cookie, cups_sid_form))
+ if ((cups_sid_form = cgiGetVariable(CUPS_SID)) == NULL)
+ cups_sid_form = cgiGetVariable("state");
+
+ if (!cups_sid_form || strcmp(cups_sid_cookie, cups_sid_form))
{
if (cups_sid_form)
fprintf(stderr, "DEBUG: " CUPS_SID " form variable is \"%s\"\n",
time_t expires, /* I - Expiration date (0 for session) */
int secure) /* I - Require SSL */
{
+ fprintf(stderr, "DEBUG2: cgiSetCookie(name=\"%s\", value=\"%s\", path=\"%s\", domain=\"%s\", expires=%ld, secure=%d)\n", name, value, path, domain, (long)expires, secure);
+
num_cookies = cupsAddOption(name, value, num_cookies, &cookies);
printf("Set-Cookie: %s=%s;", name, value);
int type; /* Authentication type */
const char *authorization; /* Pointer into Authorization string */
char *ptr, /* Pointer into string */
- bearer[2048], /* CUPS_BEARER cookie string */
+ bearer[4096], /* CUPS_BEARER cookie string */
username[HTTP_MAX_VALUE],
/* Username string */
password[HTTP_MAX_VALUE];
authorization = httpGetField(con->http, HTTP_FIELD_AUTHORIZATION);
- if (!*authorization && type == CUPSD_AUTH_BEARER && httpGetCookieValue(con->http, "CUPS_BEARER", bearer, sizeof(bearer)) && bearer[0])
+ cupsdLogClient(con, CUPSD_LOG_DEBUG2, "cookie=\"%s\"", httpGetCookie(con->http));
+
+ if (!*authorization && httpGetCookieValue(con->http, "CUPS_BEARER", bearer, sizeof(bearer)) && bearer[0])
authorization = "Bearer COOKIE";
username[0] = '\0';