Skip to content

Commit 406f5e3

Browse files
committed
Improve authentication flow
1 parent da815d8 commit 406f5e3

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

google_yubikey/__init__.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,15 @@ def parse_args():
119119
)
120120
parser_generate_key.add_argument(
121121
'-p', '--pin-policy', type=PinPolicy.from_str(PinPolicy),
122-
help='YubiKey PIN policy', default=PinPolicy.ONCE, choices=list(PinPolicy),
122+
help='YubiKey PIN policy', default=PinPolicy.DEFAULT, choices=list(PinPolicy),
123123
)
124124
parser_generate_key.add_argument(
125125
'-t', '--touch-policy', type=TouchPolicy.from_str(TouchPolicy),
126-
help='YubiKey touch policy', default=TouchPolicy.ALWAYS, choices=list(TouchPolicy),
126+
help='YubiKey touch policy', default=TouchPolicy.DEFAULT, choices=list(TouchPolicy),
127+
)
128+
parser_generate_key.add_argument(
129+
'-m', '--prompt-management-key', action='store_true',
130+
help='Prompt for management key',
127131
)
128132

129133
# "upload-key" action
@@ -154,9 +158,13 @@ def parse_args():
154158
help='Token lifetime, in seconds',
155159
)
156160
parser_token.add_argument(
157-
'-t', '--token-type', type=TokenType, choices=list(TokenType),
161+
'-t', '--token-type', type=TokenType.from_str(TokenType), choices=list(TokenType),
158162
help='Token type, in seconds', default=TokenType.ACCESS,
159163
)
164+
parser_token.add_argument(
165+
'-m', '--prompt-management-key', action='store_true',
166+
help='Prompt for management key',
167+
)
160168

161169
return parser.parse_args()
162170

@@ -167,22 +175,22 @@ def get_yubikey():
167175
return YubiKey(dev.driver)
168176

169177

170-
def authenticate(yubikey: YubiKey):
178+
def authenticate(yubikey: YubiKey, prompt_management_key: bool):
171179
""" Authenticates user to the YubiKey """
172180
print('Authenticating...')
173181
pin = getpass('Enter PIN: ')
174182
yubikey.verify(pin, touch_callback=prompt_for_touch)
175183

176-
mgmt_key = getpass('Enter management key [blank to use default key]: ')
177-
mgmt_key = mgmt_key or DEFAULT_MANAGEMENT_KEY
184+
mgmt_key = getpass('Enter management key: ') \
185+
if prompt_management_key else DEFAULT_MANAGEMENT_KEY
178186
yubikey.authenticate(mgmt_key, touch_callback=prompt_for_touch)
179187

180188

181-
def gen_private_key(yubikey: YubiKey, slot: SLOT,
189+
def gen_private_key(yubikey: YubiKey, slot: SLOT, prompt_management_key: bool,
182190
pin_policy: PIN_POLICY, touch_policy: TOUCH_POLICY,
183191
subject: str, valid_days: int):
184192
""" Generates a private key and certificate on the YubiKey """
185-
authenticate(yubikey)
193+
authenticate(yubikey, prompt_management_key)
186194

187195
print('Generating private key...')
188196
public_key = yubikey.generate_key(
@@ -231,10 +239,10 @@ def json_b64encode(obj: dict):
231239
return b64encode_str(json_str)
232240

233241

234-
def get_id_token(yubikey: YubiKey, slot: SLOT, service_account_email: str,
235-
scopes: List[str], token_lifetime: int):
242+
def get_id_token(yubikey: YubiKey, slot: SLOT, prompt_management_key: bool,
243+
service_account_email: str, scopes: List[str], token_lifetime: int):
236244
""" Generates a Google ID token with a YubiKey """
237-
authenticate(yubikey)
245+
authenticate(yubikey, prompt_management_key)
238246

239247
iat = time()
240248
header = {
@@ -277,7 +285,7 @@ def main():
277285

278286
if args.action == str(Action.GENERATE_KEY):
279287
gen_private_key(
280-
yubikey, args.slot,
288+
yubikey, args.slot, args.prompt_management_key,
281289
args.pin_policy, args.touch_policy,
282290
args.subject, args.valid_days,
283291
)
@@ -287,8 +295,8 @@ def main():
287295
print(f'Key id: {key_id}')
288296
else:
289297
id_token = get_id_token(
290-
yubikey, args.slot, args.service_account_email,
291-
args.scopes, args.token_lifetime,
298+
yubikey, args.slot, args.prompt_management_key,
299+
args.service_account_email, args.scopes, args.token_lifetime,
292300
)
293301
if args.token_type == TokenType.ACCESS:
294302
print(get_access_token(id_token))

0 commit comments

Comments
 (0)