1# The MIT License (MIT)
2#
3# Copyright (c) 2019 Looker Data Sciences, Inc.
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22
23"""AuthToken
24"""
25from typing import Optional, Type, Union
26import datetime
27
28import attr
29
30from looker_sdk.rtl import auth_session
31from looker_sdk.rtl import model
32
33
34# Same as the Looker API access token object
35# Re-declared here to be independent of model generation
36@attr.s(auto_attribs=True, init=False)
37class AccessToken(model.Model):
38 """
39 Attributes:
40 access_token: Access Token used for API calls
41 token_type: Type of Token
42 expires_in: Number of seconds before the token expires
43 refresh_token: Refresh token which can be used to obtain a new access token
44 """
45
46 access_token: Optional[str] = None
47 token_type: Optional[str] = None
48 expires_in: Optional[int] = None
49 refresh_token: Optional[str] = None
50
51 def __init__(
52 self,
53 *,
54 access_token: Optional[str] = None,
55 token_type: Optional[str] = None,
56 expires_in: Optional[int] = None,
57 refresh_token: Optional[str] = None,
58 ):
59 self.access_token = access_token
60 self.token_type = token_type
61 self.expires_in = expires_in
62 self.refresh_token = refresh_token
63
64
65class AuthToken:
66 """Used to instantiate or check expiry of an AccessToken object"""
67
68 def __init__(
69 self, token: Optional[AccessToken] = None,
70 ):
71 self.lag_time = 10
72 self.access_token: str = ""
73 self.refresh_token: str = ""
74 self.token_type: str = ""
75 self.expires_in: int = 0
76 self.expires_at = datetime.datetime.now() + datetime.timedelta(
77 seconds=-self.lag_time
78 )
79 if token is None:
80 token = AccessToken()
81 self.set_token(token)
82
83 def set_token(self, token: AccessToken):
84 """Assign the token and set its expiration."""
85 self.access_token = token.access_token or ""
86 if isinstance(token, AccessToken):
87 self.refresh_token = token.refresh_token or ""
88 self.token_type = token.token_type or ""
89 self.expires_in = token.expires_in or 0
90
91 lag = datetime.timedelta(seconds=-self.lag_time)
92 if token.access_token and token.expires_in:
93 lag = datetime.timedelta(seconds=token.expires_in - self.lag_time)
94 self.expires_at = datetime.datetime.now() + lag
95
96 @property
97 def is_active(self) -> bool:
98 """True if authentication token has not timed out"""
99 if not self.expires_at:
100 return False
101 return self.expires_at > datetime.datetime.now()