<commit_before>__author__ = "<NAME> <<EMAIL>>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"

# singleton
_instance = None

def astroprintCloud():
	global _instance
	if _instance is None:
		_instance = AstroPrintCloud()
	return _instance

import requests
import hmac
import binascii
import uuid
import os
import json

from urllib import quote_plus
from os.path import splitext, split
from hashlib import sha256

from time import sleep

from requests_toolbelt import MultipartEncoder

from flask import current_app
from flask.ext.login import login_user, logout_user, current_user
from flask.ext.principal import Identity, identity_changed, AnonymousIdentity

from octoprint.settings import settings
from octoprint.server import userManager
from octoprint.events import eventManager, Events

from astroprint.software import softwareManager
from astroprint.boxrouter import boxrouterManager

class HMACAuth(requests.auth.AuthBase):
	def __init__(self, publicKey, privateKey):
		self.publicKey = publicKey
		self.privateKey = privateKey

	def __call__(self, r):
		r.headers['User-Agent'] = softwareManager().userAgent
		sig_base = '&'.join((r.method, r.headers['User-Agent']))
		hashed = hmac.new(self.privateKey, sig_base, sha256)

		r.headers['X-Public'] = self.publicKey
		r.headers['X-Hash'] = binascii.b2a_base64(hashed.digest())[:-1]

		return r

class AstroPrintCloud(object):
	def __init__(self):
		self.settings = settings()

		if current_user and current_user.is_authenticated():
			publicKey = current_user.publicKey
			privateKey = current_user.privateKey
		else:
			publicKey = None
			privateKey = None		

		self.hmacAuth = HMACAuth(publicKey, privateKey,)
		self.apiHost = self.settings.get(['cloudSlicer', 'apiHost'])
		self._print_file_store = None
		self._sm = softwareManager()
		self._gcodeMgr = None

	@staticmethod
	def cloud_enabled():
		s = settings()
		u = current_user

		if not u.is_authenticated():
			return False
		else:
			return s.get(['cloudSlicer', 'apiHost']) and u.privateKey and u.publicKey

	def signin(self, email, password):
		from octoprint.server import userManager
		from astroprint.network import networkManager

		user = None
		userLoggedIn = False

		if networkManager().isOnline():
			private_key = self.get_private_key(email, password)

			if private_key:
				public_key = self.get_public_key(email, private_key)

				if public_key:
					#The signin was successful
					self.settings.set(["cloudSlicer", "loggedUser"], email)
					self.settings.save()
					boxrouterManager().boxrouter_connect()

					#Let's protect the box now:
					user = userManager.findUser(email)

					if user:
						userManager.changeUserPassword(email, password)
						userManager.changeCloudAccessKeys(email, public_key, private_key)
					else:
						user = userManager.addUser(email, password, public_key, private_key, True)

					userLoggedIn = True

		else:
			user = userManager.findUser(email)
			userLoggedIn = user is not None and user.check_password(userManager.createPasswordHash(password))

		if userLoggedIn:
			login_user(user, remember=True)
			userId = user.get_id()
			identity_changed.send(current_app._get_current_object(), identity=Identity(userId))
			eventManager().fire(Events.LOCK_STATUS_CHANGED, userId)

			#let the singleton be recreated again, so new credentials are taken into use
			global _instance
			_instance = None

			return True

		return False

	def signout(self):
		logout_user()

		self.settings.set(["cloudSlicer", "loggedUser"], None)<commit_msg>storing email when offline<commit_after>__author__ = "<NAME> <<EMAIL>>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"

# singleton
_instance = None

def astroprintCloud():
	global _instance
	if _instance is None:
		_instance = AstroPrintCloud()
	return _instance

import requests
import hmac
import binascii
import uuid
import os
import json

from urllib import quote_plus
from os.path import splitext, split
from hashlib import sha256

from time import sleep

from requests_toolbelt import MultipartEncoder

from flask import current_app
from flask.ext.login import login_user, logout_user, current_user
from flask.ext.principal import Identity, identity_changed, AnonymousIdentity

from octoprint.settings import settings
from octoprint.server import userManager
from octoprint.events import eventManager, Events

from astroprint.software import softwareManager
from astroprint.boxrouter import boxrouterManager

class HMACAuth(requests.auth.AuthBase):
	def __init__(self, publicKey, privateKey):
		self.publicKey = publicKey
		self.privateKey = privateKey

	def __call__(self, r):
		r.headers['User-Agent'] = softwareManager().userAgent
		sig_base = '&'.join((r.method, r.headers['User-Agent']))
		hashed = hmac.new(self.privateKey, sig_base, sha256)

		r.headers['X-Public'] = self.publicKey
		r.headers['X-Hash'] = binascii.b2a_base64(hashed.digest())[:-1]

		return r

class AstroPrintCloud(object):
	def __init__(self):
		self.settings = settings()

		if current_user and current_user.is_authenticated():
			publicKey = current_user.publicKey
			privateKey = current_user.privateKey
		else:
			publicKey = None
			privateKey = None		

		self.hmacAuth = HMACAuth(publicKey, privateKey,)
		self.apiHost = self.settings.get(['cloudSlicer', 'apiHost'])
		self._print_file_store = None
		self._sm = softwareManager()
		self._gcodeMgr = None

	@staticmethod
	def cloud_enabled():
		s = settings()
		u = current_user

		if not u.is_authenticated():
			return False
		else:
			return s.get(['cloudSlicer', 'apiHost']) and u.privateKey and u.publicKey

	def signin(self, email, password):
		from octoprint.server import userManager
		from astroprint.network import networkManager

		user = None
		userLoggedIn = False

		if networkManager().isOnline():
			private_key = self.get_private_key(email, password)

			if private_key:
				public_key = self.get_public_key(email, private_key)

				if public_key:
					#The signin was successful
					self.settings.set(["cloudSlicer", "loggedUser"], email)
					self.settings.save()
					boxrouterManager().boxrouter_connect()

					#Let's protect the box now:
					user = userManager.findUser(email)

					if user:
						userManager.changeUserPassword(email, password)
						userManager.changeCloudAccessKeys(email, public_key, private_key)
					else:
						user = userManager.addUser(email, password, public_key, private_key, True)

					userLoggedIn = True

		else:
			user = userManager.findUser(email)
			if user and user.check_password(userManager.createPasswordHash(password)):
				userLoggedIn = True
				self.settings.set(["cloudSlicer", "loggedUser"], email)
				self.settings.save()

		if userLoggedIn:
			login_user(user, remember=True)
			userId = user.get_id()
			identity_changed.send(current_app._get_current_object(), identity=Identity(userId))
			eventManager().fire(Events.LOCK_STATUS_CHANGED, userId)

			#let the singleton be recreated again, so new credentials are taken into use
			global _instance
			_instance = None

			return True

		return False

	def signout(self):
		logout_user()

		self.settings.set(["cloudSlicer", "loggedUser"], None)
{'fraction_non_alphanumeric': 0.09459647173218502, 'fraction_numerical': 0.002778163633838033, 'mean_word_length': 4.664830841856806, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5413707', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2221, 'n_words': 567}