Show More
@@ -1,140 +1,140 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # App Enlight Enterprise Edition, including its added features, Support |
|
19 | 19 | # services, and proprietary license terms, please see |
|
20 | 20 | # https://rhodecode.com/licenses/ |
|
21 | 21 | |
|
22 | 22 | import json |
|
23 | 23 | |
|
24 | 24 | from pyramid.security import unauthenticated_userid |
|
25 | 25 | |
|
26 | 26 | import appenlight.lib.helpers as helpers |
|
27 | 27 | |
|
28 | 28 | from authomatic.providers import oauth2, oauth1 |
|
29 | 29 | from authomatic import Authomatic |
|
30 | 30 | from appenlight.models.user import User |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class CSRFException(Exception): |
|
34 | 34 | pass |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | class JSONException(Exception): |
|
38 | 38 | pass |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | def get_csrf_token(request): |
|
42 | 42 | return request.session.get_csrf_token() |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | def safe_json_body(request): |
|
46 | 46 | """ |
|
47 | 47 | Returns None if json body is missing or erroneous |
|
48 | 48 | """ |
|
49 | 49 | try: |
|
50 | 50 | return request.json_body |
|
51 | 51 | except ValueError: |
|
52 | 52 | return None |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | def unsafe_json_body(request): |
|
56 | 56 | """ |
|
57 | 57 | Throws JSONException if json can't deserialize |
|
58 | 58 | """ |
|
59 | 59 | try: |
|
60 | 60 | return request.json_body |
|
61 | 61 | except ValueError: |
|
62 | 62 | raise JSONException('Incorrect JSON') |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | def get_user(request): |
|
66 | 66 | if not request.path_info.startswith('/static'): |
|
67 | 67 | user_id = unauthenticated_userid(request) |
|
68 | 68 | try: |
|
69 | 69 | user_id = int(user_id) |
|
70 | 70 | except Exception: |
|
71 | 71 | return None |
|
72 | 72 | |
|
73 | 73 | if user_id: |
|
74 | 74 | user = User.by_id(user_id) |
|
75 | 75 | if user: |
|
76 | 76 | request.environ['appenlight.username'] = '%d:%s' % ( |
|
77 | 77 | user_id, user.user_name) |
|
78 | 78 | return user |
|
79 | 79 | else: |
|
80 | 80 | return None |
|
81 | 81 | |
|
82 | 82 | |
|
83 | 83 | def es_conn(request): |
|
84 | 84 | return request.registry.es_conn |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | def add_flash_to_headers(request, clear=True): |
|
88 | 88 | """ |
|
89 | 89 | Adds pending flash messages to response, if clear is true clears out the |
|
90 | 90 | flash queue |
|
91 | 91 | """ |
|
92 | 92 | flash_msgs = helpers.get_type_formatted_flash(request) |
|
93 | 93 | request.response.headers['x-flash-messages'] = json.dumps(flash_msgs) |
|
94 | 94 | helpers.clear_flash(request) |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | def get_authomatic(request): |
|
98 | 98 | settings = request.registry.settings |
|
99 | 99 | # authomatic social auth |
|
100 | 100 | authomatic_conf = { |
|
101 | 101 | # callback http://yourapp.com/social_auth/twitter |
|
102 | 102 | 'twitter': { |
|
103 | 103 | 'class_': oauth1.Twitter, |
|
104 | 104 | 'consumer_key': settings.get('authomatic.pr.twitter.key', ''), |
|
105 | 105 | 'consumer_secret': settings.get('authomatic.pr.twitter.secret', |
|
106 | 106 | ''), |
|
107 | 107 | }, |
|
108 | 108 | # callback http://yourapp.com/social_auth/facebook |
|
109 | 109 | 'facebook': { |
|
110 | 110 | 'class_': oauth2.Facebook, |
|
111 |
'consumer_key': settings.get('authomatic.pr.facebook.app_id', ' |
|
|
111 | 'consumer_key': settings.get('authomatic.pr.facebook.app_id', ''), | |
|
112 | 112 | 'consumer_secret': settings.get('authomatic.pr.facebook.secret', |
|
113 | 113 | ''), |
|
114 | 114 | 'scope': ['email'], |
|
115 | 115 | }, |
|
116 | 116 | # callback http://yourapp.com/social_auth/google |
|
117 | 117 | 'google': { |
|
118 | 118 | 'class_': oauth2.Google, |
|
119 | 119 | 'consumer_key': settings.get('authomatic.pr.google.key', ''), |
|
120 | 120 | 'consumer_secret': settings.get( |
|
121 | 121 | 'authomatic.pr.google.secret', ''), |
|
122 | 122 | 'scope': ['profile', 'email'], |
|
123 | 123 | }, |
|
124 | 124 | 'github': { |
|
125 | 125 | 'class_': oauth2.GitHub, |
|
126 | 126 | 'consumer_key': settings.get('authomatic.pr.github.key', ''), |
|
127 | 127 | 'consumer_secret': settings.get( |
|
128 | 128 | 'authomatic.pr.github.secret', ''), |
|
129 | 129 | 'scope': ['repo', 'public_repo', 'user:email'], |
|
130 | 130 | 'access_headers': {'User-Agent': 'AppEnlight'}, |
|
131 | 131 | }, |
|
132 | 132 | 'bitbucket': { |
|
133 | 133 | 'class_': oauth1.Bitbucket, |
|
134 | 134 | 'consumer_key': settings.get('authomatic.pr.bitbucket.key', ''), |
|
135 | 135 | 'consumer_secret': settings.get( |
|
136 | 136 | 'authomatic.pr.bitbucket.secret', '') |
|
137 | 137 | } |
|
138 | 138 | } |
|
139 | 139 | return Authomatic( |
|
140 | 140 | config=authomatic_conf, secret=settings['authomatic.secret']) |
General Comments 0
You need to be logged in to leave comments.
Login now