From 2757cbdd892d032a3bfe148b73a2dfc37bde0a63 Mon Sep 17 00:00:00 2001 From: victorsndvg Date: Tue, 26 Dec 2017 09:04:47 +0100 Subject: [PATCH 1/2] Add Fiware backend --- shub/apps/base/context_processors.py | 2 + shub/apps/users/static/css/social-auth.css | 48 ++++++++++ .../static/img/social-buttons/fiware.png | Bin 0 -> 3626 bytes .../img/social-buttons/fiware_hover.png | Bin 0 -> 2798 bytes shub/apps/users/templates/social/login.html | 4 + shub/backends/__init__.py | 0 shub/backends/keyrock.py | 83 ++++++++++++++++++ shub/settings/auth.py | 2 +- shub/settings/config.py | 2 + shub/settings/dummy_secrets.py | 10 +++ 10 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 shub/apps/users/static/img/social-buttons/fiware.png create mode 100644 shub/apps/users/static/img/social-buttons/fiware_hover.png create mode 100644 shub/backends/__init__.py create mode 100644 shub/backends/keyrock.py diff --git a/shub/apps/base/context_processors.py b/shub/apps/base/context_processors.py index d223a8eb..876280dd 100644 --- a/shub/apps/base/context_processors.py +++ b/shub/apps/base/context_processors.py @@ -27,6 +27,7 @@ ENABLE_TWITTER_AUTH, ENABLE_GITHUB_AUTH, ENABLE_GITLAB_AUTH, + ENABLE_FIWARE_AUTH, HELP_CONTACT_EMAIL, HELP_INSTITUTION_SITE, PRIVATE_ONLY, @@ -55,6 +56,7 @@ def auth_processor(request): "ENABLE_TWITTER_AUTH":ENABLE_TWITTER_AUTH, "ENABLE_GITHUB_AUTH":ENABLE_GITHUB_AUTH, "ENABLE_GITLAB_AUTH":ENABLE_GITLAB_AUTH, + "ENABLE_FIWARE_AUTH":ENABLE_FIWARE_AUTH, "PLUGINS_ENABLED":PLUGINS_ENABLED,} diff --git a/shub/apps/users/static/css/social-auth.css b/shub/apps/users/static/css/social-auth.css index 6bb8ee89..b93bf394 100644 --- a/shub/apps/users/static/css/social-auth.css +++ b/shub/apps/users/static/css/social-auth.css @@ -219,3 +219,51 @@ html * { transition: all .3s ease; } +/* Fiware */ + +/* +.blue{color:#0093C6;} rgb(0, 147, 198) +.blue-ligth{color:#009FD6;} rgb(0, 159, 214) +.blue-dark{color:#3E80AD;} rgb(62, 128, 173) +*/ + +#fiware-connect { + background: rgb(255, 255, 255) url('/static/img/social-buttons/fiware.png') no-repeat scroll 5px 1px / 45px 45px padding-box border-box; + border: 1px solid rgb(62, 128, 173); + color: rgb(62, 128, 173); +} + +#fiware-connect:hover { + color: rgb(255, 255, 255); + border-color: rgb(62, 128, 173); + background: rgb(62, 128, 173) url('/static/img/social-buttons/fiware_hover.png') no-repeat scroll 5px 1px / 45px 45px padding-box border-box; + -webkit-transition: all .8s ease-out; + -moz-transition: all .3s ease; + -ms-transition: all .3s ease; + -o-transition: all .3s ease; + transition: all .3s ease-out; +} + +#fiware-connect span { + box-sizing: border-box; + color: rgb(62, 128, 173); + cursor: pointer; + text-align: center; + text-transform: uppercase; + border: 0px none rgb(62, 128, 173); + outline: rgb(62, 128, 173) none 0px; + -webkit-transition: all .3s ease; + -moz-transition: all .3s ease; + -ms-transition: all .3s ease; + -o-transition: all .3s ease; + transition: all .3s ease; +} + +#fiware-connect:hover span { + color: rgb(0, 159, 214); + -webkit-transition: all .3s ease; + -moz-transition: all .3s ease; + -ms-transition: all .3s ease; + -o-transition: all .3s ease; + transition: all .3s ease; + diff --git a/shub/apps/users/static/img/social-buttons/fiware.png b/shub/apps/users/static/img/social-buttons/fiware.png new file mode 100644 index 0000000000000000000000000000000000000000..8c89ede1845218967d480cc59d3b8eda41ba29f6 GIT binary patch literal 3626 zcmaJ^c|25WA18(EOO!2ROvJU!ScVx*V#YEw_J~5m3!yu8hk$rG+QL?uvYnDhN za#i;1OSIWjNJ#Iv@9n zA8t<8JKTj*tv)JxWjNj{nLO`L2#A;5l_Pcf{4B(Dm(}Q{7V=;-$9+n zSOJh^e>^~2O;gn!41oZkP&J5_HWUg`0ce0B8tP!y4ON9`!L^}q4IRMW7m(%6ALjwL zK^gz;i*-T(y=XKFTwR?`r>oI5)yV#y>J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Cx~V{2?m)H9bM{b?Hx@2Xb>0W1T(ozrrQdPiu}W-pQ%{{`Ah|8*SuS zi|DtThSu*iCLEk3hMmo4VzyTuI>-sf*STbA8rZ7!-QRF@Y@iYD@_M>SW4vj7gGbNU zi%viX!PlYOdayPcxz=`uNyu&*edVN@wN%HG=Ug>f-Mtt3N!T~xvdN7wx;pLlj^9%J z+$rgIGYgb7cc1Svq@KrGo^@WHh2wgS@1I}D=y89jd8jDhc<-I1@e_p~#J@H6pn7tq zKd=+STfEnF+Mk`gFSa}Cb$rx8PEU;ef#`!}gr~Z9qEYg-b7lAVk}S8CLlg2^UBc6J z58E?EuZ86q3cekXSrN|EnN%~^BFMHms!$Mf#$t?9x#Gv-bCAZ&4n*CZKO4NYGWcGy zLh??rMDwbhWAiC-oWSdegEbBJZw0v`w{Nn)P`p`Av{mb? zQ7$#4-qoqO<9o}f)j+YWzY7y=KY6LmHsVQ%fQ-$d;n+;M8ER5txtPGUjvUZ8A65Bq rJt;Yup?~yS?ymihLQLil9x$8Ex=-NLcu48~-y7P<0(IZ;Qsln?8M;f6 literal 0 HcmV?d00001 diff --git a/shub/apps/users/static/img/social-buttons/fiware_hover.png b/shub/apps/users/static/img/social-buttons/fiware_hover.png new file mode 100644 index 0000000000000000000000000000000000000000..f4781cb5a775cc5fbbf03f06376b42ff1aff348d GIT binary patch literal 2798 zcmV~VV+S}aaD z?M_H#qJwpXk1p;%e`5JZ%}GLjrbsNn(%VO)WM9@K9p1bq42wpPgvph_TLbYivdjvH0erY zXced{d?c!$SD=F(IDTy`azOKIKU;3>weC54p^d;L3J~&KnR9Ydn_qjT2LLoBHJZ-j z7usQQ{P5Y1QT5={*A)HNQ5IFrq7R&|3+wGtQ@|Uq#f;o^9AEj+uzXL1Zh?sC6H^yg z&pBFiU=Qpr;`-xBv&QSL!vTO!y6-Q3Z~$PG?9jlqcwA0%*l*ZjfUCoR6GzU2sQnVS zV+`ia$igw*gCqADhO!`SX)TXB*Fxh;x8%lPZu%vBw0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yT_NoVHm&nfvBOsuRn4Lssn5}(PYI&?-%+!UwH*@{AVI;l zt&Zat)`ryPu5m~3e(^%w0-`Og6;j=%4{)aWVL04G5bLkQ?vgoyehv$ewzQT*oxEg$ z-oy{7S+=G_F;=svyCSdQCV?T1Cfn=Qi&xfxHV3~9n4WbUyNd1!HYMzPG3nZO@{EVi zRUw=bPm<`vn28zS548Ev3qIjz}II(%ro3 z<(f2o!`0Jh*2{6LOS-}&&(=QAdIJ+HK-WxNKv6wMak_cNEeS>@@({ThJ5!TIYr{z< zqYkKF`u?J;zO5YeFz(l*De@#Y#%uR#Q<4gX+o_BSpc~fIW7>`LZI6c)Nun(TfDC1? zAWgMfo-BMNQa=-OK>a{TDTC!rw012+M~KoS8lp;eB295psgP`-EAx-WVvDE(@~eT? z?RPN`dseEY!=6T zFgtFC-64a;A%@k-h22HmsIUnF5b_uZcpeCOPKbpxMEo`)kyoz}^7qq~`Cp9m*BcU0 zTtame;}QoH2NVYs2NVYs2NVYs2NVYsM-W{82cruE4_RG2oB#j-07*qoM6N<$g1?Va ARR910 literal 0 HcmV?d00001 diff --git a/shub/apps/users/templates/social/login.html b/shub/apps/users/templates/social/login.html index 55c9e1c9..5b9dc65d 100644 --- a/shub/apps/users/templates/social/login.html +++ b/shub/apps/users/templates/social/login.html @@ -44,6 +44,10 @@

Hello, {{ user.get_full_name }}!

{% endif %} + {% if ENABLE_FIWARE_AUTH %} + + {% endif %} + {% if 'ldap_auth' in PLUGINS_ENABLED %} {% endif %} diff --git a/shub/backends/__init__.py b/shub/backends/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/shub/backends/keyrock.py b/shub/backends/keyrock.py new file mode 100644 index 00000000..a7b8a8a9 --- /dev/null +++ b/shub/backends/keyrock.py @@ -0,0 +1,83 @@ +""" +Github OAuth2 backend, docs at: + https://python-social-auth.readthedocs.io/en/latest/backends/github.html +""" +from urllib.parse import urlencode +from requests import HTTPError + +from six.moves.urllib.parse import urljoin + +from social_core.backends.oauth import BaseOAuth2 +from social_core.exceptions import AuthFailed + +from django.conf import settings +from shub.logger import bot + +import base64 + + +class KeyrockOAuth2(BaseOAuth2): + """Keyrock OAuth authentication backend""" + name = 'fiware' + AUTHORIZATION_URL = urljoin( + settings.FIWARE_IDM_ENDPOINT, '/oauth2/authorize') + ACCESS_TOKEN_URL = urljoin(settings.FIWARE_IDM_ENDPOINT, '/oauth2/token') + #LOGOUT_URL = urljoin(settings.FIWARE_IDM_ENDPOINT, '/auth/logout') + ACCESS_TOKEN_METHOD = 'POST' + + REDIRECT_STATE = False + + EXTRA_DATA = [ + ('id', 'username'), + ('id', 'uid') + ] + + def get_user_id(self, details, response): + return response['id'] + + def get_user_details(self, response): + """Return user details from FI-WARE account""" + bot.debug( {'username': response.get('id'), + 'email': response.get('email') or '', + 'fullname': response.get('displayName') or ''}) + return {'username': response.get('id'), + 'email': response.get('email') or '', + 'fullname': response.get('displayName') or ''} + + def user_data(self, access_token, *args, **kwargs): + """Loads user data from service""" + url = urljoin(settings.FIWARE_IDM_ENDPOINT, '/user?' + urlencode({ + 'access_token': access_token + })) + bot.debug(self.get_json(url)) + return self.get_json(url) + + def auth_headers(self): + response = super(KeyrockOAuth2, self).auth_headers() + + keys = settings.SOCIAL_AUTH_FIWARE_KEY + \ + ":" + settings.SOCIAL_AUTH_FIWARE_SECRET + authorization_basic = base64.b64encode( + keys.encode('ascii')).decode('ascii') + response['Authorization'] = 'Basic ' + authorization_basic + + bot.debug(response) + return response + + def auth_complete_params(self, state=None): + # response = super(KeyrockOAuth2, self).auth_complete_params(state) + # response['grant_type'] = 'authorization_code' + \ + # '&code=' + response['code'] + \ + # '&redirect_uri=' + response['redirect_uri'] + # return response + + bot.debug( { + 'grant_type': 'authorization_code', # request auth code + 'code': self.data.get('code', ''), # server response code + 'redirect_uri': self.get_redirect_uri(state) + } ) + return { + 'grant_type': 'authorization_code', # request auth code + 'code': self.data.get('code', ''), # server response code + 'redirect_uri': self.get_redirect_uri(state) + } diff --git a/shub/settings/auth.py b/shub/settings/auth.py index 8186c5b0..71b46240 100644 --- a/shub/settings/auth.py +++ b/shub/settings/auth.py @@ -31,6 +31,7 @@ 'social_core.backends.facebook.FacebookOAuth2', 'social_core.backends.github.GithubOAuth2', 'guardian.backends.ObjectPermissionBackend', + 'shub.backends.keyrock.KeyrockOAuth2', ) @@ -48,7 +49,6 @@ 'social_core.pipeline.user.user_details', ) - SOCIAL_AUTH_LOGIN_REDIRECT_URL = 'http://127.0.0.1' # http://psa.matiasaguirre.net/docs/configuration/settings.html#urls-options diff --git a/shub/settings/config.py b/shub/settings/config.py index e8aa0fab..693370a6 100644 --- a/shub/settings/config.py +++ b/shub/settings/config.py @@ -24,6 +24,8 @@ ENABLE_TWITTER_AUTH=True ENABLE_GITHUB_AUTH=False ENABLE_GITLAB_AUTH=False +ENABLE_FIWARE_AUTH=False + # NOTE you will need to set autehtication methods up. # Configuration goes into secrets.py diff --git a/shub/settings/dummy_secrets.py b/shub/settings/dummy_secrets.py index 68cc5a13..564f16ed 100644 --- a/shub/settings/dummy_secrets.py +++ b/shub/settings/dummy_secrets.py @@ -57,6 +57,16 @@ #SOCIAL_AUTH_GITLAB_SECRET = '' +# ----------------------------------------------------------------------------- +# Fiware Keyrock OAuth2 +# Only required if ENABLE_FIWARE_AUTH=TRUE in config.py + +#FIWARE_IDM_ENDPOINT = 'https://account.lab.fiware.org' +#SOCIAL_AUTH_FIWARE_KEY = '' +#SOCIAL_AUTH_FIWARE_SECRET = '' + + + # ============================================================================= # Plugin Authentication From b698de19c9db34b080807bfea589f952b066805b Mon Sep 17 00:00:00 2001 From: victorsndvg Date: Mon, 12 Mar 2018 09:36:51 +0100 Subject: [PATCH 2/2] Add fiware as plugin --- shub/backends/__init__.py | 0 shub/plugins/fiware/__init__.py | 1 + shub/{backends => plugins/fiware}/keyrock.py | 0 shub/plugins/fiware/urls.py | 1 + shub/settings/auth.py | 1 - shub/settings/config.py | 3 ++- 6 files changed, 4 insertions(+), 2 deletions(-) delete mode 100644 shub/backends/__init__.py create mode 100644 shub/plugins/fiware/__init__.py rename shub/{backends => plugins/fiware}/keyrock.py (100%) create mode 100644 shub/plugins/fiware/urls.py diff --git a/shub/backends/__init__.py b/shub/backends/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/shub/plugins/fiware/__init__.py b/shub/plugins/fiware/__init__.py new file mode 100644 index 00000000..ffa63ed1 --- /dev/null +++ b/shub/plugins/fiware/__init__.py @@ -0,0 +1 @@ +AUTHENTICATION_BACKENDS = ('shub.plugins.fiware.keyrock.KeyrockOAuth2',) diff --git a/shub/backends/keyrock.py b/shub/plugins/fiware/keyrock.py similarity index 100% rename from shub/backends/keyrock.py rename to shub/plugins/fiware/keyrock.py diff --git a/shub/plugins/fiware/urls.py b/shub/plugins/fiware/urls.py new file mode 100644 index 00000000..637600f5 --- /dev/null +++ b/shub/plugins/fiware/urls.py @@ -0,0 +1 @@ +urlpatterns = [] diff --git a/shub/settings/auth.py b/shub/settings/auth.py index 69edfb25..86fbee32 100644 --- a/shub/settings/auth.py +++ b/shub/settings/auth.py @@ -31,7 +31,6 @@ 'social_core.backends.facebook.FacebookOAuth2', 'social_core.backends.github.GithubOAuth2', 'social_core.backends.gitlab.GitLabOAuth2', - 'shub.backends.keyrock.KeyrockOAuth2', ) diff --git a/shub/settings/config.py b/shub/settings/config.py index 6dad5b46..4979b37c 100644 --- a/shub/settings/config.py +++ b/shub/settings/config.py @@ -105,5 +105,6 @@ # - ldap_auth: Allows sregistry to authenitcate against an LDAP directory PLUGINS_ENABLED = [ -# 'ldap_auth' +# 'ldap_auth', +# 'fiware', ]