Skip to content

Commit cb4ea77

Browse files
committed
Provide default scopes for both authorization APIs.
1 parent d117c46 commit cb4ea77

1 file changed

Lines changed: 82 additions & 42 deletions

File tree

cups/oauth.c

Lines changed: 82 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ static const char *github_metadata = // Github.com OAuth metadata
158158
//
159159

160160
static char *oauth_copy_response(http_t *http);
161+
static char *oauth_copy_scopes(cups_json_t *metadata);
161162
static cups_json_t *oauth_do_post(const char *ep, const char *content_type, const char *data);
162163
static char *oauth_load_value(const char *auth_uri, const char *secondary_uri, _cups_otype_t otype);
163164
static char *oauth_make_path(char *buffer, size_t bufsize, const char *auth_uri, const char *secondary_uri, _cups_otype_t otype);
@@ -460,47 +461,8 @@ cupsOAuthGetAuthorizationCode(
460461

461462
if (!scopes)
462463
{
463-
cups_json_t *values; // Parameter values
464-
465-
if ((values = cupsJSONFind(metadata, "scopes_supported")) != NULL)
466-
{
467-
// Convert scopes_supported to a string...
468-
size_t i, // Looping var
469-
count, // Number of values
470-
length = 0; // Length of string
471-
cups_json_t *current; // Current value
472-
473-
for (i = 0, count = cupsJSONGetCount(values); i < count; i ++)
474-
{
475-
current = cupsJSONGetChild(values, i);
476-
477-
if (cupsJSONGetType(current) == CUPS_JTYPE_STRING)
478-
length += strlen(cupsJSONGetString(current)) + 1;
479-
}
480-
481-
if (length > 0 && (scopes_supported = malloc(length)) != NULL)
482-
{
483-
// Copy the scopes to a string with spaces between them...
484-
char *ptr; // Pointer into value
485-
486-
for (i = 0, ptr = scopes_supported; i < count; i ++)
487-
{
488-
current = cupsJSONGetChild(values, i);
489-
490-
if (cupsJSONGetType(current) == CUPS_JTYPE_STRING)
491-
{
492-
if (i)
493-
*ptr++ = ' ';
494-
495-
cupsCopyString(ptr, cupsJSONGetString(current), length - (size_t)(ptr - scopes_supported));
496-
ptr += strlen(ptr);
497-
}
498-
}
499-
500-
// Use the supported scopes in the request...
501-
scopes = scopes_supported;
502-
}
503-
}
464+
scopes_supported = oauth_copy_scopes(metadata);
465+
scopes = scopes_supported;
504466
}
505467

506468
DEBUG_printf("1cupsOAuthGetAuthorizationCode: scopes=\"%s\"", scopes);
@@ -1297,7 +1259,8 @@ cupsOAuthMakeAuthorizationURL(
12971259
char code_challenge[64]; // Hashed code verifier string
12981260
size_t num_vars = 0; // Number of form variables
12991261
cups_option_t *vars = NULL; // Form variables
1300-
char *url; // URL for authorization page
1262+
char *url, // URL for authorization page
1263+
*scopes_supported; // Supported scopes
13011264

13021265

13031266
// Range check input...
@@ -1334,7 +1297,14 @@ cupsOAuthMakeAuthorizationURL(
13341297
num_vars = cupsAddOption("resource", resource_uri, num_vars, &vars);
13351298

13361299
if (scopes)
1300+
{
13371301
num_vars = cupsAddOption("scope", scopes, num_vars, &vars);
1302+
}
1303+
else if ((scopes_supported = oauth_copy_scopes(metadata)) != NULL)
1304+
{
1305+
num_vars = cupsAddOption("scope", scopes_supported, num_vars, &vars);
1306+
free(scopes_supported);
1307+
}
13381308

13391309
if (state)
13401310
num_vars = cupsAddOption("state", state, num_vars, &vars);
@@ -1488,6 +1458,76 @@ oauth_copy_response(http_t *http) // I - HTTP connection
14881458
}
14891459

14901460

1461+
//
1462+
// 'oauth_copy_scopes()' - Copy all supported scopes from the metadata.
1463+
//
1464+
// Caller must free returned string...
1465+
//
1466+
1467+
static char * // O - Scopes
1468+
oauth_copy_scopes(
1469+
cups_json_t *metadata) // I - Metadata
1470+
{
1471+
char *scopes = NULL; // Supported scopes
1472+
cups_json_t *values; // Parameter values
1473+
1474+
1475+
if ((values = cupsJSONFind(metadata, "scopes_supported")) != NULL)
1476+
{
1477+
// Convert scopes_supported to a string...
1478+
size_t i, // Looping var
1479+
count, // Number of values
1480+
length = 0; // Length of string
1481+
cups_json_t *current; // Current value
1482+
const char *scope; // Scope string
1483+
1484+
// Figure out the total length...
1485+
for (i = 0, count = cupsJSONGetCount(values); i < count; i ++)
1486+
{
1487+
current = cupsJSONGetChild(values, i);
1488+
1489+
if (cupsJSONGetType(current) == CUPS_JTYPE_STRING)
1490+
{
1491+
// Only copy common scopes...
1492+
scope = cupsJSONGetString(current);
1493+
1494+
if (!strcmp(scope, "email") || !strcmp(scope, "profile") || !strcmp(scope, "openid"))
1495+
length += strlen(scope) + 1;
1496+
}
1497+
}
1498+
1499+
if (length > 0 && (scopes = malloc(length)) != NULL)
1500+
{
1501+
// Copy the scopes to a string with spaces between them...
1502+
char *ptr; // Pointer into value
1503+
1504+
for (i = 0, ptr = scopes; i < count; i ++)
1505+
{
1506+
// Append each scope...
1507+
current = cupsJSONGetChild(values, i);
1508+
1509+
if (cupsJSONGetType(current) == CUPS_JTYPE_STRING)
1510+
{
1511+
// Only copy common scopes...
1512+
scope = cupsJSONGetString(current);
1513+
1514+
if (!strcmp(scope, "email") || !strcmp(scope, "profile") || !strcmp(scope, "openid"))
1515+
{
1516+
if (ptr > scopes)
1517+
*ptr++ = ' ';
1518+
1519+
cupsCopyString(ptr, scope, length - (size_t)(ptr - scopes));
1520+
ptr += strlen(ptr);
1521+
}
1522+
}
1523+
}
1524+
}
1525+
}
1526+
1527+
return (scopes);
1528+
}
1529+
1530+
14911531
//
14921532
// 'oauth_do_post()' - Send a POST request with the specified data and do error
14931533
// handling, returning JSON when available.

0 commit comments

Comments
 (0)