-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapi.py
180 lines (144 loc) · 5.13 KB
/
gapi.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from email.message import EmailMessage
import base64
import secrets
import string
# If modifying these scopes, delete the file token.json.
SCOPES = [
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/admin.directory.user",
"https://www.googleapis.com/auth/admin.directory.group",
]
def generatePassword(psw_len: int = 12):
alphabet = string.ascii_letters + string.digits + string.punctuation
while True:
password = "".join(secrets.choice(alphabet) for i in range(psw_len))
if (
any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3
):
break
return password
class UserName:
familyName: str
givenName: str
displayName: str
def __init__(self, familyName: str, givenName: str, displayName: str):
self.familyName = familyName
self.givenName = givenName
self.displayName = displayName
class User:
name = UserName
password: str
primaryEmail: str
recoveryEmail: str
changePasswordAtNextLogin: bool
orgUnitPath: str = "/"
def __init__(
self,
name: UserName,
password: str,
primaryEmail: str,
recoveryEmail: str,
changePasswordAtNextLogin: bool,
orgUnitPath: str = "/",
):
self.name = name
self.password = password
self.primaryEmail = primaryEmail
self.recoveryEmail = recoveryEmail
self.changePasswordAtNextLogin = changePasswordAtNextLogin
self.orgUnitPath = orgUnitPath
def toDict(self):
return {
"name": {
"familyName": self.name.familyName,
"givenName": self.name.givenName,
"displayName": self.name.displayName,
},
"password": self.password,
"primaryEmail": self.primaryEmail,
"recoveryEmail": self.recoveryEmail,
"changePasswordAtNextLogin": self.changePasswordAtNextLogin,
"orgUnitPath": self.orgUnitPath,
}
def insert(self, service):
try:
res = service.users().insert(body=self.toDict()).execute()
return res
except HttpError as error:
print("An error occurred: %s" % error)
def sendGmail(service, message: EmailMessage) -> dict:
encoded_message = base64.urlsafe_b64encode(message.as_bytes()).decode()
body = {"raw": encoded_message}
try:
res = service.users().messages().send(userId="me", body=body).execute()
return res
except HttpError as error:
print("An error occurred: %s" % error)
def creds() -> Credentials:
creds = None
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
return creds
def getGmailService(creds: Credentials):
return build("gmail", "v1", credentials=creds)
def getAdminService(creds: Credentials):
return build("admin", "directory_v1", credentials=creds)
def main():
creds = creds()
try:
admin = build("admin", "directory_v1", credentials=creds)
gmail = build("gmail", "v1", credentials=creds)
# Call the Admin SDK Directory API
# Insert dum user
user = User(
UserName("Doe", "John", "John Doe"),
generatePassword(),
True,
)
user.insert(admin)
message = EmailMessage()
message["To"] = "[email protected]"
message["From"] = "[email protected]"
message["Subject"] = "Automated draft"
message["content-type"] = "text/html"
message.set_content(
f"""
One dumb user has been created:
- First name: {user.name.givenName}
- Last name: {user.name.familyName}
- Display name: {user.name.displayName}
- Primary email: {user.primaryEmail}
- Recovery email: {user.recoveryEmail}
- Password: {user.password}
- Change password at next login: {user.changePasswordAtNextLogin}
"""
)
sendGmail(gmail, message)
except HttpError as error:
print("An error occurred: %s" % error)
if __name__ == "__main__":
main()