Introduction
Introduction Statistics Contact Development Disclaimer Help
auth.py - toot - Unnamed repository; edit this file 'description' to name the r…
Log
Files
Refs
LICENSE
---
auth.py (3069B)
---
1 # -*- coding: utf-8 -*-
2
3 import webbrowser
4
5 from builtins import input
6 from getpass import getpass
7
8 from toot import api, config, DEFAULT_INSTANCE, User, App
9 from toot.exceptions import ApiError, ConsoleError
10 from toot.output import print_out
11
12
13 def register_app(domain):
14 print_out("Looking up instance info...")
15 instance = api.get_instance(domain)
16
17 print_out("Found instance <blue>{}</blue> running Mastodon version <…
18 instance['title'], instance['version']))
19
20 try:
21 print_out("Registering application...")
22 response = api.create_app(domain)
23 except ApiError:
24 raise ConsoleError("Registration failed.")
25
26 base_url = 'https://' + domain
27
28 app = App(domain, base_url, response['client_id'], response['client_…
29 config.save_app(app)
30
31 print_out("Application tokens saved.")
32
33 return app
34
35
36 def create_app_interactive(instance=None):
37 if not instance:
38 print_out("Choose an instance [<green>{}</green>]: ".format(DEFA…
39 instance = input()
40 if not instance:
41 instance = DEFAULT_INSTANCE
42
43 return config.load_app(instance) or register_app(instance)
44
45
46 def create_user(app, access_token):
47 # Username is not yet known at this point, so fetch it from Mastodon
48 user = User(app.instance, None, access_token)
49 creds = api.verify_credentials(app, user)
50
51 user = User(app.instance, creds['username'], access_token)
52 config.save_user(user, activate=True)
53
54 print_out("Access token saved to config at: <green>{}</green>".forma…
55 config.get_config_file_path()))
56
57 return user
58
59
60 def login_interactive(app, email=None):
61 print_out("Log in to <green>{}</green>".format(app.instance))
62
63 if email:
64 print_out("Email: <green>{}</green>".format(email))
65
66 while not email:
67 email = input('Email: ')
68
69 password = getpass('Password: ')
70
71 try:
72 print_out("Authenticating...")
73 response = api.login(app, email, password)
74 except ApiError:
75 raise ConsoleError("Login failed")
76
77 return create_user(app, response['access_token'])
78
79
80 BROWSER_LOGIN_EXPLANATION = """
81 This authentication method requires you to log into your Mastodon instan…
82 in your browser, where you will be asked to authorize <yellow>toot</yell…
83 your account. When you do, you will be given an <yellow>authorization co…
84 which you need to paste here.
85 """
86
87
88 def login_browser_interactive(app):
89 url = api.get_browser_login_url(app)
90 print_out(BROWSER_LOGIN_EXPLANATION)
91
92 print_out("This is the login URL:")
93 print_out(url)
94 print_out("")
95
96 yesno = input("Open link in default browser? [Y/n]")
97 if not yesno or yesno.lower() == 'y':
98 webbrowser.open(url)
99
100 authorization_code = ""
101 while not authorization_code:
102 authorization_code = input("Authorization code: ")
103
104 print_out("\nRequesting access token...")
105 response = api.request_access_token(app, authorization_code)
106
107 return create_user(app, response['access_token'])
You are viewing proxied material from vernunftzentrum.de. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.