-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
auth.py
47 lines (34 loc) · 1.35 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -----------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -----------------------------------------------------------------------------
"""
Authentication methods
"""
from knack.util import CLIError
from paconn.authentication.profile import Profile
from paconn.authentication.tokenmanager import TokenManager
def get_authentication(settings, force_authenticate):
"""
Logs the user in and saves the token in a file.
"""
tokenmanager = TokenManager()
credentials = tokenmanager.read()
token_expired = TokenManager.is_expired(credentials)
# Get new token
if token_expired or force_authenticate:
profile = Profile(
client_id=settings.client_id,
tenant=settings.tenant,
resource=settings.resource,
authority_url=settings.authority_url)
credentials = profile.authenticate_device_code()
tokenmanager.write(credentials)
token_expired = TokenManager.is_expired(credentials)
# Couldn't acquire valid token
if token_expired:
raise CLIError('Couldn\'t get authentication')
def remove_authentication():
tokenmanager = TokenManager()
tokenmanager.delete_token_file()