26 |
26 |
27 |
27 |
28 class Client(db.Model): |
28 class Client(db.Model): |
29 client_id = db.Column(db.String(40), primary_key=True) |
29 client_id = db.Column(db.String(40), primary_key=True) |
30 client_secret = db.Column(db.String(55), nullable=False) |
30 client_secret = db.Column(db.String(55), nullable=False) |
31 |
31 client_type = db.Column(db.String(12), nullable=False, default='public') |
|
32 |
|
33 user_id = db.Column(db.ForeignKey('user.id')) |
|
34 user = db.relationship('User') |
|
35 |
32 _redirect_uris = db.Column(db.Text) |
36 _redirect_uris = db.Column(db.Text) |
33 _default_scopes = db.Column(db.Text) |
37 _default_scopes = db.Column(db.Text) |
34 |
|
35 @property |
|
36 def client_type(self): |
|
37 return 'public' |
|
38 |
38 |
39 @property |
39 @property |
40 def redirect_uris(self): |
40 def redirect_uris(self): |
41 if self._redirect_uris: |
41 if self._redirect_uris: |
42 return self._redirect_uris.split() |
42 return self._redirect_uris.split() |
101 |
101 |
102 # currently only bearer is supported |
102 # currently only bearer is supported |
103 token_type = db.Column(db.String(40)) |
103 token_type = db.Column(db.String(40)) |
104 |
104 |
105 access_token = db.Column(db.String(255), unique=True) |
105 access_token = db.Column(db.String(255), unique=True) |
106 refresh_token = db.Column(db.String(255), unique=True) |
106 refresh_token = db.Column(db.String(255), unique=True, nullable=True) |
107 expires = db.Column(db.DateTime) |
107 expires = db.Column(db.DateTime) |
108 _scopes = db.Column(db.Text) |
108 _scopes = db.Column(db.Text) |
109 |
109 |
110 @property |
110 @property |
111 def scopes(self): |
111 def scopes(self): |
133 session['id'] = user.id |
134 session['id'] = user.id |
134 return redirect('/') |
135 return redirect('/') |
135 user = current_user() |
136 user = current_user() |
136 return render_template('oauth/home.html', user=user) |
137 return render_template('oauth/home.html', user=user) |
137 |
138 |
138 def generate_credentials(redirect_uris): |
|
139 item = Client( |
|
140 client_id=gen_salt(40), |
|
141 client_secret=gen_salt(50), |
|
142 _redirect_uris=' '.join(redirect_uris), |
|
143 _default_scopes='basic', |
|
144 ) |
|
145 db.session.add(item) |
|
146 db.session.commit() |
|
147 return jsonify( |
|
148 client_id=item.client_id, |
|
149 client_secret=item.client_secret, |
|
150 ) |
|
151 |
|
152 @app.route('/get-client-credentials') |
|
153 def make_client_credentials(): |
|
154 return generate_credentials(app.config.get("CLIENT_REDIRECT_URIS", [])) |
|
155 |
|
156 @app.route('/get-renkan-credentials') |
|
157 def make_renkan_credentials(): |
|
158 return generate_credentials(app.config.get("RENKAN_REDIRECT_URIS", [])) |
|
159 |
|
160 @oauth.clientgetter |
139 @oauth.clientgetter |
161 def load_client(client_id): |
140 def load_client(client_id): |
162 return Client.query.filter_by(client_id=client_id).first() |
141 return Client.query.filter_by(client_id=client_id).first() |
163 |
142 |
164 |
143 |
205 expires_in = token.pop('expires_in') |
184 expires_in = token.pop('expires_in') |
206 expires = datetime.utcnow() + timedelta(seconds=expires_in) |
185 expires = datetime.utcnow() + timedelta(seconds=expires_in) |
207 |
186 |
208 tok = Token( |
187 tok = Token( |
209 access_token=token['access_token'], |
188 access_token=token['access_token'], |
210 refresh_token=token['refresh_token'], |
|
211 token_type=token['token_type'], |
189 token_type=token['token_type'], |
212 _scopes=token['scope'], |
190 _scopes=token['scope'], |
213 expires=expires, |
191 expires=expires, |
214 client_id=request.client.client_id, |
192 client_id=request.client.client_id, |
215 user_id=request.user.id, |
193 user_id=request.user.id, |
217 db.session.add(tok) |
195 db.session.add(tok) |
218 db.session.commit() |
196 db.session.commit() |
219 return tok |
197 return tok |
220 |
198 |
221 |
199 |
222 @app.route('/oauth/token', methods=['GET', 'POST']) |
200 @app.route('/oauth/oauth2/token', methods=['GET', 'POST']) |
223 @oauth.token_handler |
201 @oauth.token_handler |
224 def access_token(): |
202 def access_token(): |
225 return None |
203 return None |
226 |
204 |
227 |
205 |
228 @app.route('/oauth/authorize', methods=['GET', 'POST']) |
206 @app.route('/oauth/oauth2/authorize', methods=['GET', 'POST']) |
229 @oauth.authorize_handler |
207 @oauth.authorize_handler |
230 def authorize(*args, **kwargs): |
208 def authorize(*args, **kwargs): |
|
209 print(request.headers) |
231 user = current_user() |
210 user = current_user() |
232 if not user: |
211 if not user: |
233 return redirect('/') |
212 return redirect('/') |
234 if request.method == 'GET': |
213 if request.method == 'GET': |
235 client_id = kwargs.get('client_id') |
214 client_id = kwargs.get('client_id') |
240 |
219 |
241 confirm = request.form.get('confirm', 'no') |
220 confirm = request.form.get('confirm', 'no') |
242 return confirm == 'yes' |
221 return confirm == 'yes' |
243 |
222 |
244 |
223 |
245 @app.route('/api/me') |
224 @app.route('/rest/user/InfoComplete') |
246 @oauth.require_oauth() |
225 @oauth.require_oauth() |
247 def me(): |
226 def user_info(): |
248 user = request.oauth.user |
227 user = request.oauth.user |
249 return jsonify(id=user.id, username=user.username) |
228 return jsonify(id=user.id, username=user.username) |
250 |
229 |
|
230 @app.route('/rest/oauth/validate/<token>') |
|
231 def validate_token(token): |
|
232 print(request.headers) |
|
233 database_token = Token.query.filter_by(access_token=token).first() |
|
234 related_client = database_token.client |
|
235 return jsonify( |
|
236 access_token=token, |
|
237 redirect_uri= related_client.redirect_uris, |
|
238 error=0, |
|
239 description= "", |
|
240 scope=database_token.scopes |
|
241 ) |
|
242 |
|
243 def init_client(client_id, client_secret, redirect_uris, client_owner, confidential=False): |
|
244 client = Client.query.filter_by(client_id=client_id, client_secret=client_secret).first() |
|
245 if not client: |
|
246 print("Creating client for "+client_owner) |
|
247 user = User.query.filter_by(username=client_owner).first() |
|
248 if not user: |
|
249 user = User(username=username) |
|
250 db.session.add(user) |
|
251 db.session.commit() |
|
252 if confidential: |
|
253 type="confidential" |
|
254 else: |
|
255 type="public" |
|
256 client = Client( |
|
257 client_id=client_id, |
|
258 client_secret=client_secret, |
|
259 _redirect_uris=' '.join(redirect_uris), |
|
260 _default_scopes='basic', |
|
261 user_id=user.id, |
|
262 client_type=type |
|
263 ) |
|
264 db.session.add(client) |
|
265 db.session.commit() |
251 |
266 |
252 if __name__ == '__main__': |
267 if __name__ == '__main__': |
253 db.create_all() |
268 db.create_all() |
|
269 init_client( |
|
270 client_id=app.config["RENKAN_CLIENT_ID"], |
|
271 client_secret=app.config["RENKAN_CLIENT_SECRET"], |
|
272 redirect_uris=app.config["RENKAN_REDIRECT_URIS"], |
|
273 client_owner=app.config["RENKAN_SERVER_USER"] |
|
274 ) |
|
275 init_client( |
|
276 client_id=app.config["MOCK_GED_CLIENT_ID"], |
|
277 client_secret=app.config["MOCK_GED_CLIENT_SECRET"], |
|
278 redirect_uris=app.config["MOCK_GED_REDIRECT_URIS"], |
|
279 client_owner=app.config["MOCK_GED_SERVER_USER"], |
|
280 confidential=True |
|
281 ) |
254 app.run() |
282 app.run() |