Advertisement
NickG

YggAuth

Oct 16th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. import urllib.request as request
  2. from urllib.error import HTTPError
  3. import json
  4.  
  5. class YggAuth:
  6.     def __init__(self,
  7.         client_token=None,
  8.         access_token=None,
  9.         username=None,
  10.         password=None
  11.     ):
  12.         self.client_token = client_token
  13.         self.access_token = (access_token
  14.             if self.is_valid_access(access_token) else None)
  15.         self.username = username
  16.         self.password = password
  17.  
  18.     def _gen_req(self, endpoint, payload):
  19.         url = 'https://authserver.mojang.com' + endpoint
  20.         data = json.dumps(payload).encode('utf-8')
  21.         headers = {'Content-Type': 'application/json'}
  22.         return request.Request(url, data, headers, method='POST')
  23.  
  24.     def _gen_rep(self, req):
  25.         try:
  26.             rep = request.urlopen(req)
  27.         except HTTPError as reperr:
  28.             rep = reperr
  29.         data = rep.read().decode('utf-8')
  30.         return json.loads(data) if data else None
  31.  
  32.     def _ygg_req(self, endpoint, payload):
  33.         return self._gen_rep(self._gen_req(endpoint, payload))
  34.  
  35.     #Generate an access token using a username and password
  36.     #(Any existing client token is invalidated if not provided)
  37.     def login(self, username=None, password=None, client_token=None):
  38.         endpoint = '/authenticate'
  39.         payload = {
  40.             'agent': {
  41.                 'name': 'Minecraft',
  42.                 'version': 1,
  43.             },
  44.         }
  45.         if username:
  46.             self.username = username
  47.         payload['username'] = self.username
  48.         if password:
  49.             self.password = password
  50.         payload['password'] = self.password
  51.         if client_token:
  52.             self.client_token = client_token
  53.         if self.client_token:
  54.             payload['clientToken'] = self.client_token
  55.         rep = self._ygg_req(endpoint, payload)
  56.         if 'error' not in rep:
  57.             self.access_token = rep['accessToken']
  58.             self.client_token = rep['clientToken']
  59.         return rep
  60.  
  61.     #Generate an access token with a client/access token pair
  62.     #(The used access token is invalidated)
  63.     def refresh(self, client_token=None, access_token=None):
  64.         endpoint = '/refresh'
  65.         payload = {}
  66.         payload['accessToken'] = access_token if access_token else self.access_token
  67.         payload['clientToken'] = client_token if client_token else self.client_token
  68.         rep = self._ygg_req(endpoint, payload)
  69.         if 'error' not in rep:
  70.             self.access_token = rep['accessToken']
  71.             self.client_token = rep['clientToken']
  72.         return rep
  73.  
  74.     #Invalidate access tokens with a username and password
  75.     def signout(self, username=None, password=None):
  76.         endpoint = '/signout'
  77.         payload = {}
  78.         payload['username'] = username if username else self.username
  79.         payload['password'] = password if password else self.password
  80.         return self._ygg_req(endpoint, payload)
  81.  
  82.     #Invalidate access tokens with a client/access token pair
  83.     def invalidate(self, client_token=None, access_token=None):
  84.         endpoint = '/invalidate'
  85.         payload = {}
  86.         payload['accessToken'] = access_token if access_token else self.access_token
  87.         payload['clientToken'] = client_token if client_token else self.client_token
  88.         return self._ygg_req(endpoint, payload)
  89.  
  90.     #Check if an access token is valid
  91.     def is_valid_access(self, access_token=None):
  92.         endpoint = '/validate'
  93.         payload = {'accessToken': access_token}
  94.         return False if self._ygg_req(endpoint, payload) else True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement