Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to new check-pin API #74

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions server/weblogin_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from threading import Timer
from datetime import timedelta

from flask import Flask, Response, request, Markup, session, render_template
from flask import Flask, Response, request, session, render_template
from markupsafe import Markup
from flask_pyoidc import OIDCAuthentication
from flask_pyoidc.provider_configuration import ProviderConfiguration, ClientMetadata
from flask_pyoidc.user_session import UserSession
Expand Down Expand Up @@ -185,12 +186,12 @@ def check_pin():
reply = {
'result': 'SUCCESS',
'username': user_id,
'groups': {
'coaaa': 'A CO with the beautiful name AAA',
'cobbb': 'A CO named BBB',
'coccc': 'A CO named CCC!',
'coddd': 'A CO named DDD?',
},
'groups': [
{'short_name': 'coaaa', 'name': 'A CO with the beautiful name AAA'},
{'short_name': 'cobbb', 'name': 'A CO named BBB'},
{'short_name': 'coccc', 'name': 'A CO named CCC!'},
{'short_name': 'coddd', 'name': 'A CO named DDD?'},
],
'info': f'Authenticated on attribute {attribute}'
}
cached[user_id] = True
Expand Down
14 changes: 10 additions & 4 deletions src/pam_weblogin.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,10 @@ PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, UNUSED int flags, int arg
errno = 0;
tty_output(pamh, MSG_GROUPS);
for (unsigned int i=0; i < max_groups; i++) {
char *value = getValue(pam_groups, i);
tty_output(pamh, str_printf(" [%d] %s", i+1, value));
char *name = getString(
getIndex(pam_groups, i)
, "name");
tty_output(pamh, str_printf(" [%d] %s", i+1, name));
}
char *group_input = tty_input(pamh, PROMPT_GROUP, PAM_PROMPT_ECHO_ON);
group = strtol(group_input, &end, 10);
Expand All @@ -224,13 +226,17 @@ PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, UNUSED int flags, int arg
tty_output(pamh, PROMPT_WRONG_NUMBER);
} else
{
pam_group = getKey(pam_groups, (unsigned int)(group - 1));
pam_group = getString(
getIndex(pam_groups, (unsigned int)(group - 1))
, "short_name");
break;
}
}
} else // max_groups <= 1;
{
pam_group = getKey(pam_groups, 0);
pam_group = getString(
getIndex(pam_groups, 0)
, "short_name");
}
log_message(LOG_INFO, "PAM Group: %s", pam_group);
} else // no pam_groups
Expand Down
4 changes: 0 additions & 4 deletions src/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ char *tty_input(pam_handle_t *pamh, const char *text, int echo_code)
*resp->resp == '\000')
{
log_message(LOG_ERR, "Did not receive input from user");
if (retval == PAM_SUCCESS && resp && resp->resp)
{
ret = resp->resp;
}
}
else
{
Expand Down
11 changes: 9 additions & 2 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "tty.h"
#include "utils.h"

json_value *findKey(json_value* value, const char* name) {
json_value *findKey(json_value* value, const char* name)
{
json_value * result = NULL;

if (value == NULL || name == NULL || name[0]=='\0' ) {
Expand Down Expand Up @@ -35,6 +36,11 @@ json_value *findKey(json_value* value, const char* name) {
return result;
}

json_value *getIndex(json_value* value, const unsigned int index)
{
return (index < value->u.array.length) ? value->u.array.values[index] : NULL;
}

char *getString(json_value *value, const char *name)
{
json_value *key = findKey(value, name);
Expand All @@ -57,7 +63,8 @@ bool getBool(json_value *value, const char *name)
return key ? key->u.boolean : false;
}

char *str_printf(const char * fmt, ...) {
char *str_printf(const char * fmt, ...)
{
char *buffer = NULL;
va_list args;

Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
#include <json-parser/json.h>

json_value *findKey(json_value *value, const char *name);
json_value *getIndex(json_value* value, const unsigned int index);
char *getString(json_value *, const char *name);
char *getKey(json_value *value, const unsigned int index);
char *getValue(json_value *value, const unsigned int index);
bool getBool(json_value *value, const char *name);
char *str_printf(const char *fmt, ...)
__attribute__ ((format (printf, 1, 2)));

char *trim(char *s, const size_t len);


Expand Down
13 changes: 12 additions & 1 deletion tests/test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ const char json_txt[] = "{ " \
"\"key_int\": 1, " \
"\"key_str\": \"test\", " \
"\"key_str_empty\": \"\", " \
"\"key_obj\": { \"key_sub\": \"val_sub\" } " \
"\"key_obj\": { \"key_sub\": \"val_sub\" }, " \
"\"key_array\": [ \"one\", \"two\" ] " \
"}";


Expand Down Expand Up @@ -222,6 +223,16 @@ START_TEST(test_json_utils)
char *result4 = getValue(findKey(json, "key_obj"), 0);
ck_assert_str_eq(result4, "val_sub");

/* get first index */
json_value *item1 = getIndex(findKey(json, "key_array"), 0);
char *result5 = item1->u.string.ptr;
ck_assert_str_eq(result5, "one");

/* get second index */
json_value *item2 = getIndex(findKey(json, "key_array"), 1);
char *result6 = item2->u.string.ptr;
ck_assert_str_eq(result6, "two");

/* convenience function to get string or bool */
ck_assert_int_eq(getBool(json, "key_true"), true);
ck_assert_int_eq(getBool(json, "key_false"), false);
Expand Down