Skip to content

Commit

Permalink
add --bearer and --auth-header options to oidc-token
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmann committed Aug 27, 2024
1 parent 838aa7e commit 79c7b49
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

- Added possibility to add custom request parameters to requests done by the agent. This is done through
a `custom_parameters.config` file placed in the agent dir or `/etc/oidc-agent`
- Added the `--bearer` and `--auth-header` options to `oidc-token`. These can be used to ease api calls.

### Change / Enhancement / Bugfix

Expand Down
8 changes: 7 additions & 1 deletion src/oidc-token/oidc-token.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ int main(int argc, char** argv) {
} else if ((arguments.expiration_env.useIt + arguments.token_env.useIt +
arguments.issuer_env.useIt) ==
0) { // none of these options specified
printf("%s\n", res.token);
if (arguments.bearer) {
printf("Bearer %s\n", res.token);
} else if (arguments.auth_header) {
printf("Authorization: Bearer %s", res.token);
} else {
printf("%s\n", res.token);
}
} else { // only one option specified
if (arguments.issuer_env.useIt) {
if (arguments.issuer_env.str == NULL) {
Expand Down
15 changes: 15 additions & 0 deletions src/oidc-token/oidc-token_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define OPT_NAME 2
#define OPT_AUDIENCE 3
#define OPT_IDTOKEN 4
#define OPT_BEARER 5
#define OPT_AUTHHEADER 6

static struct argp_option options[] = {
{0, 0, 0, 0, "General:", 1},
Expand Down Expand Up @@ -48,6 +50,15 @@ static struct argp_option options[] = {
1},
{"force-new", 'f', 0, 0,
"Forces that a new access token is issued and returned.", 1},
{"bearer", OPT_BEARER, 0, 0,
"Returns the token in the bearer format that is suitable as the value"
" for an http authorization header",
1},
{"auth-header", OPT_AUTHHEADER, 0, 0,
"Returns the token included in a http authorization header for usage "
"with e.g. curl or httpie.",
1},
{"auth", OPT_AUTHHEADER, 0, OPTION_ALIAS, NULL, 1},

{0, 0, 0, 0, "Advanced:", 2},
{"scope", 's', "SCOPE", 0,
Expand Down Expand Up @@ -107,6 +118,8 @@ static error_t parse_opt(int key, char* arg, struct argp_state* state) {
case OPT_IDTOKEN: arguments->idtoken = 1; break;
case OPT_NAME: arguments->application_name = arg; break;
case OPT_AUDIENCE: arguments->audience = arg; break;
case OPT_BEARER: arguments->bearer = 1; break;
case OPT_AUTHHEADER: arguments->auth_header = 1; break;
case 'i':
arguments->issuer_env.str = arg;
arguments->issuer_env.useIt = 1;
Expand Down Expand Up @@ -174,4 +187,6 @@ void initArguments(struct arguments* arguments) {
arguments->printAll = 0;
arguments->idtoken = 0;
arguments->forceNewToken = 0;
arguments->bearer = 0;
arguments->auth_header = 0;
}
2 changes: 2 additions & 0 deletions src/oidc-token/oidc-token_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct arguments {
unsigned char printAll;
unsigned char idtoken;
unsigned char forceNewToken;
unsigned char bearer;
unsigned char auth_header;

time_t min_valid_period;
};
Expand Down

0 comments on commit 79c7b49

Please sign in to comment.