Skip to content

Commit

Permalink
Wait 20 seconds before polling
Browse files Browse the repository at this point in the history
Only attempt three times before exiting

Bug: #39
  • Loading branch information
Jnchi committed Jun 10, 2019
1 parent 73406ee commit 5090ce0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pam_aad.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
#define CONFIG_FILE "/etc/pam_aad.conf"
#define DEBUG false
#define HOST "https://login.microsoftonline.com/"
#define MAX_ATTEMPTS 3
#define RESOURCE "https://graph.microsoft.com/"
#define SUBJECT "Your one-time passcode for signing in via Azure Active Directory"
#define TTW 5 /* time to wait in seconds */
#define TTW 20 /* time to wait in seconds */
#define USER_AGENT "azure_authenticator_pam/1.0"
#define USER_PROMPT "An email with a one-time passcode was sent to your email." \
"\nEnter the code at https://aka.ms/devicelogin, then press enter.\n"
Expand Down Expand Up @@ -180,6 +181,7 @@ static void auth_bearer_request(struct ret_data *data,
bool debug)
{
const char *auth_bearer;
int attempts = 0;

sds endpoint = sdsnew(HOST);
endpoint = sdscat(endpoint, "common/oauth2/token");
Expand All @@ -192,10 +194,8 @@ static void auth_bearer_request(struct ret_data *data,
post_body = sdscat(post_body, "&grant_type=device_code");

for (;;) {
nanosleep((const struct timespec[]) { {
TTW, 0 }
}, NULL);
json_data = curl(endpoint, post_body, NULL, debug);
attempts++;

if (json_object_get(json_data, "access_token")) {
auth_bearer =
Expand All @@ -206,9 +206,12 @@ static void auth_bearer_request(struct ret_data *data,
json_string_value(json_object_get(json_data, "error"));
}

if (strcmp(auth_bearer, AUTH_ERROR) != 0)
if ((strcmp(auth_bearer, AUTH_ERROR) != 0) || attempts >= MAX_ATTEMPTS)
break;

nanosleep((const struct timespec[]) { {
TTW, 0 }
}, NULL);
}

sdsfree(endpoint);
Expand Down

0 comments on commit 5090ce0

Please sign in to comment.