Debian Codegraph @

src/oidc-gen/promptAndSet/scope.c

raw
1 #include "_helper.h"
2 #include "account/account.h"
3 #include "account/issuer_helper.h"
4 #include "defines/agent_values.h"
5 #include "defines/oidc_values.h"
6 #include "defines/settings.h"
7 #include "oidc-gen/gen_handler.h"
8 #include "promptAndSet.h"
9 #include "utils/string/stringUtils.h"
10
11 char* getSupportedScopes(struct oidc_account* account,
12 const struct arguments* arguments) {
13 if (arguments->usePublicClient) {
14 char* pubScopes = getScopesForPublicClient(account);
15 if (strValid(pubScopes)) {
16 return pubScopes;
17 }
18 }
19 if (compIssuerUrls(account_getIssuerUrl(account), ELIXIR_ISSUER_URL)) {
20 return oidc_strcopy(ELIXIR_SUPPORTED_SCOPES);
21 }
22 return gen_handleScopeLookup(account_getIssuerUrl(account),
23 account_getCertPath(account));
24 }
25
26 void askOrNeedScope(struct oidc_account* account,
27 const struct arguments* arguments, int optional) {
28 if (readScope(account, arguments)) {
29 if (strequal(account_getScope(account), AGENT_SCOPE_ALL)) {
30 account_setScope(account, getSupportedScopes(account, arguments));
31 }
32 return;
33 }
34 ERROR_IF_NO_PROMPT(optional, ERROR_MESSAGE("scope", OPT_LONG_SCOPE));
35 char* supportedScope = getSupportedScopes(account, arguments);
36 printNormal("The following scopes are supported: %s\n", supportedScope);
37 if (!strValid(account_getScope(account))) {
38 account_setScope(account, oidc_strcopy(DEFAULT_SCOPE));
39 }
40 char* res = _gen_promptMultipleSpaceSeparated(
41 "Scopes or 'max'", account_getScope(account), optional);
42 if (res) {
43 account_setScopeExact(account, res);
44 }
45 if (strequal(account_getScope(account), AGENT_SCOPE_ALL)) {
46 account_setScope(account, supportedScope);
47 } else {
48 secFree(supportedScope);
49 }
50 }
51
52 int readScope(struct oidc_account* account, const struct arguments* arguments) {
53 if (arguments->scope) {
54 void (*setter)(struct oidc_account*, char*) = account_setScope;
55 if (strequal(arguments->scope, AGENT_SCOPE_ALL)) {
56 setter = account_setScopeExact;
57 }
58 setter(account, oidc_strcopy(arguments->scope));
59 return 1;
60 }
61 if (prompt_mode() == 0 && strValid(account_getScope(account))) {
62 return 1;
63 }
64 return 0;
65 }
66
67 void askScope(struct oidc_account* account, const struct arguments* arguments) {
68 return askOrNeedScope(account, arguments, 1);
69 }
70
71 void needScope(struct oidc_account* account,
72 const struct arguments* arguments) {
73 return askOrNeedScope(account, arguments, 0);
74 }