def get_protected_resource(): """ Representerer en beskyttet ressurs. Krever en gyldig token i Authorization: Bearer-headeren. """ auth_header = request.headers.get('Authorization') if not auth_header or auth_header[:7] != 'Bearer ': return 'Ikke logget inn', 403 session = get_session(auth_header[7:]) # Fjern "Bearer " og hent ut token if not session: return 'Ikke logget inn', 403 else: return 'Du er logget inn som {} og har tilgang!'.format(session)
def setup(args): """ Similar to config but creates AWS resources using cfn template and based on the cfn stack output, creates the specs file for the user, then writes petctl config. """ region = args.region s3_bucket_name = args.s3_bucket efs_id = args.efs_id os.makedirs(PETCTL_DIR, exist_ok=True) session = auth.get_session(region) cfn = CloudFormation(session) cfn.create_specs_file(SPECS_FILE, s3_bucket_name, efs_id) write_config_file(region, SPECS_FILE) log.info(f"Setup complete. petctl config file: {PETCTL_CONFIG_FILE}")
def datatable(): if not request.is_json: abort(400, "Data not in JSON format!") return received = request.json try: draw = received['draw'] limit = received['length'] offset = received['start'] except KeyError: abort(400, description="Required parameters are missing!") return user_id = auth.get_session(request) data = db.get_all_records(g.dbhandle.cursor(), user_id, offset, limit) recordCount = db.get_count(g.dbhandle.cursor(),user_id) result = { 'draw': draw, 'recordsFiltered': recordCount, 'recordsTotal': recordCount, 'data': data } return jsonify(result)
def render_fullmap(): user_id = auth.get_session(request) #start_coords = (settings.DEFAULT_DEVICE_LAT, settings.DEFAULT_DEVICE_LNG) #folium_map = folium.Map(location=start_coords, zoom_start=14) cursor = g.dbhandle.cursor() markers = [] maxlat, maxlng, minlat, minlng = 0, 0, 10**32, 10*32 for device in db.get_last_loc(cursor, user_id): #for device in db.deg_fake_last_loc(): #fake device for tests. Test OK markers.append(folium.Marker([device['Latitude'], device['Longitude']])) maxlat = max(maxlat, device['Latitude']) maxlng = max(maxlng, device['Longitude']) minlat = min(maxlat, device['Latitude']) minlng = min(maxlng, device['Longitude']) lat = (minlat + maxlat) / 2 lng = (minlng + maxlng) / 2 folium_map = folium.Map(location=(lat, lng), zoom_start=10) for marker in markers: marker.add_to(folium_map) return folium_map._repr_html_()
import sys, tidylib, os import auth try: session = auth.get_session(os.getenv("POPCLIP_OPTION_AUTHSECRET")) except: exit(2) # use html in preference to plain text body = os.getenv("POPCLIP_HTML") or os.getenv("POPCLIP_TEXT") # add page title if we know it title = os.getenv("POPCLIP_BROWSER_TITLE") if title: body = "<title>%s</title" % title + body # add source link if we know it source = os.getenv("POPCLIP_BROWSER_URL") if source: body += '<p>Clipped from: <a href="%s">%s</a></p>' % (source, source) # run the html through tidy html, errors = tidylib.tidy_document(body, {"char-encoding": "utf8"}) # do the job r = session.post("pages", files={"Presentation": ("Clipping", html, "text/html")}) if r.status_code != 201: exit(1)
from __future__ import print_function import os, auth, trello, traceback, sys try: session = auth.get_session(os.getenv('POPCLIP_OPTION_AUTHSECRET')); trello.verify_login(session); except: exit(2) try: trello.add_card( session=session, board_url=os.getenv('POPCLIP_OPTION_BOARD'), text=os.getenv('POPCLIP_TEXT'), position={'Top of list': 'top', 'Bottom of list': 'bottom'}.get(os.getenv('POPCLIP_OPTION_POSITION'), 'top'), source_url=os.getenv('POPCLIP_BROWSER_URL')) except trello.TrelloError as e: traceback.print_exc(file=sys.stderr) print('Status code', e.code, file=sys.stderr) if e.code == 400 or e.code == 401 or e.code == 404: exit(3) # probably bad board id else: exit(1) # who knows
from __future__ import print_function import sys, tidylib, os, json import auth, requests try: session = auth.get_session(json.loads(os.getenv('POPCLIP_OPTION_AUTHSECRET'))) except requests.exceptions.ConnectionError: exit(1) except: exit(2) # use html in preference to plain text body = os.getenv('POPCLIP_HTML') or os.getenv('POPCLIP_TEXT') # add page title if we know it title = os.getenv('POPCLIP_BROWSER_TITLE') if title: body = '<title>%s</title' % title + body # add source link if we know it source = os.getenv('POPCLIP_BROWSER_URL') if source: body += '<p>Clipped from: <a href="%s">%s</a></p>' % (source, source) # run the html through tidy html, errors = tidylib.tidy_document(body, {'char-encoding': 'utf8'}) # do the job r = session.post('pages', files={'Presentation': ('Clipping', html, 'text/html')}) if r.status_code != 201: exit(1)
def load_configuration(): if os.path.isfile(PETCTL_CONFIG_FILE): with open(PETCTL_CONFIG_FILE) as f: return json.load(f) else: return {} if __name__ == "__main__": logging.basicConfig( level=logging.INFO, format="[%(levelname)s] %(asctime)s %(module)s: %(message)s" ) petctl_configs = load_configuration() args = parse_arguments(sys.argv, **petctl_configs) if args.command == "setup": args = parse_arguments(sys.argv) setup(args) elif args.command == "configure": configure(args) else: log.info( f"{PETCTL_CONFIG_FILE} not found or is empty," f" consider running: petctl setup|configure" ) region = args.region specs_json = load_specs_json(args.specs_file) session = auth.get_session(region) args.func(session, specs_json, args)
def decorated_function(*args, **kwargs): if auth.get_session(request): return original_function(*args, **kwargs) return redirect(url_for('login', next=request.url))
import auth import requests import os cookies = auth.get_session() hueUrl = 'https://'+ cookies.list_domains()[0] moduleUrl = hueUrl + '/filebrowser' def listdir(args): path = "" options = [] if len(args) == 1: path = args[0] else: options = args[:-1] path = args[-1] flags = {"h": False, "l": False, "s": False} for option in options: if option.startswith("-"): for i in option[1:]: flags[i] = True #its a flag url = moduleUrl + "/listdir=" + path +"?format=json" r = requests.get(url, cookies=cookies) res = r.json() #print res
def get_config(key): res = os.getenv(key) # if not res: # testdata = { # 'POPCLIP_OPTION_AUTHSECRET': '""', # 'POPCLIP_TEXT': "<p>test text abc fdf" # } # if key in testdata: # res = testdata[key] return res try: session = auth.get_session( json.loads(get_config('POPCLIP_OPTION_AUTHSECRET'))) except requests.exceptions.ConnectionError: exit(1) except: exit(2) # use html in preference to plain text body = get_config('POPCLIP_HTML') or get_config('POPCLIP_TEXT') # add page title if we know it title = get_config('POPCLIP_BROWSER_TITLE') if title: body = '<title>%s</title' % title + body # add source link if we know it source = get_config('POPCLIP_BROWSER_URL')
#html, errors = tidylib.tidy_document(body, {'char-encoding': 'utf8'}) # libtidy exception on elcap return subprocess.check_output(['/usr/bin/php', 'tidy.php', text]) def get_config(key): res = os.getenv(key) # if not res: # testdata = { # 'POPCLIP_OPTION_AUTHSECRET': '""', # 'POPCLIP_TEXT': "<p>test text abc fdf" # } # if key in testdata: # res = testdata[key] return res; try: session = auth.get_session(json.loads(get_config('POPCLIP_OPTION_AUTHSECRET'))) except requests.exceptions.ConnectionError: exit(1) except: exit(2) # use html in preference to plain text body = get_config('POPCLIP_HTML') or get_config('POPCLIP_TEXT') # add page title if we know it title = get_config('POPCLIP_BROWSER_TITLE') if title: body = '<title>%s</title' % title + body # add source link if we know it source = get_config('POPCLIP_BROWSER_URL')
from __future__ import print_function import os, auth, trello, traceback, sys testauth = '["6cf5c3fef687f4cf33a6f903c4c29ad541669bf0f45d5afb0639a7f08dd2de54", "4f52bea83d785c980eabff894b65c70c"]' testdata = { 'session': auth.get_session(testauth), 'board_url': 'https://trello.com/b/7aElDmQJ/xxxxx-test', 'text': 'testing 123456', 'position': 'Top', 'source_url': 'http://example.com' } try: if os.getenv('POPCLIP_TEXT'): trello.add_card(session=auth.get_session( os.getenv('POPCLIP_OPTION_AUTHSECRET')), board_url=os.getenv('POPCLIP_OPTION_BOARD'), text=os.getenv('POPCLIP_TEXT'), position=os.getenv('POPCLIP_OPTION_POSITION'), source_url=os.getenv('POPCLIP_BROWSER_URL')) else: trello.add_card(**testdata) except trello.TrelloError as e: traceback.print_exc(file=sys.stderr) print('Status code', e.code, file=sys.stderr) if e.code == 401: exit(2) # bad auth elif e.code == 404 or e.code == 400: exit(3) # probably bad board id else:
from __future__ import print_function import os, auth, trello, traceback, sys testauth = '["6cf5c3fef687f4cf33a6f903c4c29ad541669bf0f45d5afb0639a7f08dd2de54", "4f52bea83d785c980eabff894b65c70c"]' testdata = { 'session': auth.get_session(testauth), 'board_url': 'https://trello.com/b/7aElDmQJ/xxxxx-test', 'text': 'testing 123456', 'position': 'Top', 'source_url': 'http://example.com' } try: if os.getenv('POPCLIP_TEXT'): trello.add_card( session=auth.get_session(os.getenv('POPCLIP_OPTION_AUTHSECRET')), board_url=os.getenv('POPCLIP_OPTION_BOARD'), text=os.getenv('POPCLIP_TEXT'), position=os.getenv('POPCLIP_OPTION_POSITION'), source_url=os.getenv('POPCLIP_BROWSER_URL')) else: trello.add_card(**testdata) except trello.TrelloError as e: traceback.print_exc(file=sys.stderr) print('Status code', e.code, file=sys.stderr) if e.code == 401: exit(2) # bad auth elif e.code == 404 or e.code == 400: exit(3) # probably bad board id else:
from __future__ import print_function import os, auth, trello, traceback, sys try: session = auth.get_session(os.getenv('POPCLIP_OPTION_AUTHSECRET')) trello.verify_login(session) except: exit(2) try: trello.add_card(session=session, board_url=os.getenv('POPCLIP_OPTION_BOARD'), text=os.getenv('POPCLIP_TEXT'), position={ 'Top of list': 'top', 'Bottom of list': 'bottom' }.get(os.getenv('POPCLIP_OPTION_POSITION'), 'top'), source_url=os.getenv('POPCLIP_BROWSER_URL')) except trello.TrelloError as e: traceback.print_exc(file=sys.stderr) print('Status code', e.code, file=sys.stderr) if e.code == 400 or e.code == 401 or e.code == 404: exit(3) # probably bad board id else: exit(1) # who knows