Skip to content

Commit 0e5a8cc

Browse files
committed
Remove OpenSesame OSF information
1 parent 15d3e3d commit 0e5a8cc

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

QOpenScienceFramework/connection.py

+15
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def create_session():
6969
scope=scope,
7070
redirect_uri=redirect_uri,
7171
)
72+
return session
7273

7374
# Generate correct URLs
7475
auth_url = base_url + "oauth2/authorize"
@@ -101,8 +102,16 @@ def api_call(command, *args):
101102
"""
102103
return api_base_url + api_calls[command].format(*args)
103104

105+
def check_for_active_session():
106+
""" Checks if a session object has been created and returns an Error otherwise."""
107+
if session is None:
108+
raise RuntimeError("Session is not yet initialized, use connection."
109+
"session = connection.create_session()")
110+
111+
104112
#%%--------------------------- Oauth communiucation ----------------------------
105113

114+
106115
def get_authorization_url():
107116
""" Generate the URL at which an OAuth2 token for the OSF can be requested
108117
with which OpenSesame can be allowed access to the user's account.
@@ -111,23 +120,28 @@ def get_authorization_url():
111120
-------
112121
string : The complete uri for the api endpoint
113122
"""
123+
check_for_active_session()
114124
return session.authorization_url(auth_url)
115125

116126
def parse_token_from_url(url):
117127
""" Parse token from url fragment """
128+
check_for_active_session()
118129
token = session.token_from_fragment(url)
119130
# Call logged_in function to notify event listeners that user is logged in
120131
if is_authorized():
121132
return token
122133
else:
123134
logging.debug("ERROR: Token received, but user not authorized")
124135

136+
125137
def is_authorized():
126138
""" Convenience function simply returning OAuth2Session.authorized. """
139+
check_for_active_session()
127140
return session.authorized
128141

129142
def token_valid():
130143
""" Checks if OAuth token is present, and if so, if it has not expired yet. """
144+
check_for_active_session()
131145
if not hasattr(session,"token") or not session.token:
132146
return False
133147
return session.token["expires_at"] > time.time()
@@ -204,6 +218,7 @@ def func_wrapper(*args, **kwargs):
204218
def logout():
205219
""" Logs out the user, and resets the global session object. """
206220
global session
221+
check_for_active_session()
207222
resp = session.post(logout_url,{
208223
"token": session.access_token
209224
})

example.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717
import os
1818
import logging
19+
import tempfile
1920
logging.basicConfig(level=logging.INFO)
2021

2122
# Required QT classes
@@ -26,6 +27,11 @@
2627
# Event dispatcher and listeners
2728
from QOpenScienceFramework.manager import ConnectionManager
2829

30+
####### CONFIGURE THE CLIENT ID AND REDIRECT URI HERE. REGISTER AT OSF.IO ######
31+
client_id = "<YOUR_CLIENT_ID_HERE>"
32+
redirect_uri = "<YOUR_REDIRECT_URI_HERE>"
33+
################################################################################
34+
2935
class InvalidateButton(QtWidgets.QWidget):
3036
""" Just a button to tamper with the OSF session and see what the app does
3137
to recover from missing authentication information """
@@ -47,7 +53,25 @@ class StandAlone(object):
4753
purposes. """
4854

4955
def __init__(self):
50-
tokenfile = os.path.abspath("token.json")
56+
# Check if client_id and redirect_uri have been changed
57+
if client_id == "<YOUR_CLIENT_ID_HERE>":
58+
raise RuntimeError("Please enter the client_id you have registered"
59+
" for your app at the OSF")
60+
if redirect_uri == "<YOUR_REDIRECT_URI_HERE>":
61+
raise RuntimeError("Please enter the redirect uri you have registered"
62+
" for your app at the OSF")
63+
64+
# Set OSF server settings
65+
server_settings = {
66+
"client_id" : client_id,
67+
"redirect_uri" : redirect_uri,
68+
}
69+
# Add these settings to the general settings
70+
osf.settings.update(server_settings)
71+
osf.create_session()
72+
73+
tmp_dir = tempfile.gettempdir()
74+
tokenfile = os.path.join(tmp_dir, u"osf_token.json")
5175
# Create manager object
5276
self.manager = ConnectionManager(tokenfile=tokenfile)
5377

0 commit comments

Comments
 (0)