| 1 |
#include "_helper.h" |
| 2 |
#include "account/account.h" |
| 3 |
#include "account/issuer_helper.h" |
| 4 |
#include "defines/settings.h" |
| 5 |
#include "promptAndSet.h" |
| 6 |
#include "utils/file_io/file_io.h" |
| 7 |
#include "utils/file_io/oidc_file_io.h" |
| 8 |
#include "utils/listUtils.h" |
| 9 |
#include "utils/prompt.h" |
| 10 |
#include "utils/string/stringUtils.h" |
| 11 |
|
| 12 |
void _useSuggestedIssuer(struct oidc_account* account, int optional) { |
| 13 |
list_t* issuers = getSuggestableIssuers(); |
| 14 |
size_t favPos = getFavIssuer(account, issuers); |
| 15 |
char* iss = promptSelect("Please select issuer", "Issuer", issuers, favPos, |
| 16 |
CLI_PROMPT_NOT_VERBOSE); |
| 17 |
secFreeList(issuers); |
| 18 |
if (!strValid(iss)) { |
| 19 |
printError("Something went wrong. Invalid Issuer.\n"); |
| 20 |
if (optional) { |
| 21 |
return; |
| 22 |
} |
| 23 |
exit(EXIT_FAILURE); |
| 24 |
} |
| 25 |
account_setIssuerUrl(account, iss); |
| 26 |
} |
| 27 |
|
| 28 |
void askOrNeedIssuer(struct oidc_account* account, |
| 29 |
const struct arguments* arguments, int optional) { |
| 30 |
if (readIssuer(account, arguments)) { |
| 31 |
stringifyIssuerUrl(account); |
| 32 |
return; |
| 33 |
} |
| 34 |
ERROR_IF_NO_PROMPT(optional, ERROR_MESSAGE("issuer url", OPT_LONG_ISSUER)); |
| 35 |
if (!oidcFileDoesExist(ISSUER_CONFIG_FILENAME) && |
| 36 |
!fileDoesExist(ETC_ISSUER_CONFIG_FILE)) { |
| 37 |
char* res = |
| 38 |
_gen_prompt("Issuer", account_getIssuerUrl(account), 0, optional); |
| 39 |
if (res) { |
| 40 |
account_setIssuerUrl(account, res); |
| 41 |
} |
| 42 |
} else { |
| 43 |
_useSuggestedIssuer(account, optional); |
| 44 |
} |
| 45 |
stringifyIssuerUrl(account); |
| 46 |
} |
| 47 |
|
| 48 |
int readIssuer(struct oidc_account* account, |
| 49 |
const struct arguments* arguments) { |
| 50 |
if (arguments->issuer) { |
| 51 |
account_setIssuerUrl(account, oidc_strcopy(arguments->issuer)); |
| 52 |
return 1; |
| 53 |
} |
| 54 |
if (prompt_mode() == 0 && strValid(account_getIssuerUrl(account))) { |
| 55 |
return 1; |
| 56 |
} |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
void askIssuer(struct oidc_account* account, |
| 61 |
const struct arguments* arguments) { |
| 62 |
return askOrNeedIssuer(account, arguments, 1); |
| 63 |
} |
| 64 |
|
| 65 |
void needIssuer(struct oidc_account* account, |
| 66 |
const struct arguments* arguments) { |
| 67 |
return askOrNeedIssuer(account, arguments, 0); |
| 68 |
} |