-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathauth.py
More file actions
97 lines (78 loc) · 2.26 KB
/
Copy pathauth.py
File metadata and controls
97 lines (78 loc) · 2.26 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Various classes for Gogs authentication
"""
from gogs_client.entities import json_get
class Authentication(object):
"""
An "abstract" parent class. Represents a
means of authenticating oneself to Gogs
"""
def update_kwargs(self, kwargs):
"""
Updates kwargs to include this object's authentication information
:param dict kwargs: dictionary of keyword arguments to pass to a function from
the requests module
:return: Updated kwargs
"""
raise NotImplementedError() # must be implemented by subclasses
class Token(Authentication):
"""
An immutable representation of a Gogs authentication token
"""
def __init__(self, token, name=None):
"""
:param str token: contents of Gogs authentication token
"""
self._token = token
self._name = name
@staticmethod
def from_json(parsed_json):
name = json_get(parsed_json, "name")
sha1 = json_get(parsed_json, "sha1")
return Token(sha1, name)
@property
def name(self):
"""
The name of the token
:rtype: str
"""
return self._name
@property
def token(self):
"""
The contents of the Gogs authentication token
:rtype: str
"""
return self._token
def update_kwargs(self, kwargs):
if "params" in kwargs:
kwargs["params"]["token"] = self._token
else:
kwargs["params"] = {"token": self._token}
class UsernamePassword(Authentication):
"""
An immutable representation of a Gogs username/password combination
"""
def __init__(self, username, password):
"""
:param str username: Username for Gogs account
:param str password: Password for Gogs account
"""
self._username = username
self._password = password
@property
def username(self):
"""
Username for Gogs account
:rtype: str
"""
return self._username
@property
def password(self):
"""
Password for Gogs account
:rtype: str
"""
return self._password
def update_kwargs(self, kwargs):
kwargs["auth"] = (self._username, self._password)