Uploaded image for project: 'Help-Desk'
  1. Help-Desk
  2. HELP-8955

[fiware-stackoverflow] FIWARE Authentication in Python

    Details

      Description

      Created question in FIWARE Q/A platform on 02-09-2016 at 16:09
      Please, ANSWER this question AT https://stackoverflow.com/questions/39295231/fiware-authentication-in-python

      Question:
      FIWARE Authentication in Python

      Description:
      I am trying to authenticate user using FIWARE. It returns a 404 when I request the token, but I don't have problems to get access code request.
      My code:

      class OAuth2(object):
      def _init_(self):
      self.client_id = "<client_id>"
      self.client_secret = "<client_secret>"
      self.site = 'http://0.0.0.0:8000'
      self.redirect_uri = "http://192.168.99.101:8000/auth"
      self.authorization_url = '/oauth2/authorize'
      self.token_url = '/oauth2/token'

      def authorize_url(self, **kwargs):
      oauth_params =

      {'response_type': 'code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id}

      oauth_params.update(kwargs)
      return "%s%s?%s" % (self.site, quote(self.authorization_url), urlencode(oauth_params))

      def get_token(self, code, **kwargs):
      url = "%s%s" % (self.site, quote(self.token_url))
      data =

      {'grant_type': 'authorization_code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code}

      data.update(kwargs)
      response = requests.post(url, data=data)
      content = response.content

      if isinstance(response.content, str):
      try:
      content = json.loads(response.content)
      except ValueError:
      content = parse_qs(response.content)
      else:
      return content

      In my app, I call authorize_url() to get the code.

      @app.route("/authenticate")
      def authenticate():
      auth_url = auth_app.authorize_url()
      return redirect(auth_url)

      After, I get the code by callback url and I call the get_token() method:

      @app.route('/auth', methods=['GET', 'POST'])
      def auth():
      error = request.args.get('error', '')
      if error:
      return "Error: " + error

      code = request.args.get('code')
      content = auth_app.get_token()
      return render_template('index.html', content="content: " + content)

      Github Project: https://github.com/I-am-Gabi/security-app/tree/master/2-BasicAuthentication/securityapp-ui/web

      OAuth2 class: https://github.com/I-am-Gabi/security-app/blob/master/2-BasicAuthentication/securityapp-ui/web/oauth_fiware.py

      App: https://github.com/I-am-Gabi/security-app/blob/master/2-BasicAuthentication/securityapp-ui/web/app.py

      Fiware wiki: https://github.com/ging/fiware-idm/wiki/using-the-fiware-lab-instance

        Activity

        backlogmanager Backlog Manager created issue -
        backlogmanager Backlog Manager made changes -
        Field Original Value New Value
        Component/s FIWARE-TECH-HELP [ 10278 ]
        backlogmanager Backlog Manager made changes -
        Status Open [ 1 ] In Progress [ 3 ]
        backlogmanager Backlog Manager made changes -
        Resolution Done [ 10000 ]
        Status In Progress [ 3 ] Closed [ 6 ]
        fla Fernando Lopez made changes -
        HD-Enabler KeyRock [ 10889 ]
        Description
        Created question in FIWARE Q/A platform on 02-09-2016 at 16:09
        {color: red}Please, ANSWER this question AT{color} https://stackoverflow.com/questions/39295231/fiware-authentication-in-python


        +Question:+
        FIWARE Authentication in Python

        +Description:+
        I am trying to authenticate user using FIWARE. It returns a 404 when I request the token, but I don't have problems to get access code request.
        My code:

        class OAuth2(object):
            def __init__(self):
                self.client_id = "&lt;client_id&gt;"
                self.client_secret = "&lt;client_secret&gt;"
                self.site = 'http://0.0.0.0:8000&#39;
                self.redirect_uri = "http://192.168.99.101:8000/auth"
                self.authorization_url = '/oauth2/authorize'
                self.token_url = '/oauth2/token'

            def authorize_url(self, **kwargs):
                oauth_params = {'response_type': 'code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id}
                oauth_params.update(kwargs)
                return "%s%s?%s" % (self.site, quote(self.authorization_url), urlencode(oauth_params))

            def get_token(self, code, **kwargs):
                url = "%s%s" % (self.site, quote(self.token_url))
                data = {'grant_type': 'authorization_code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code}
                data.update(kwargs)
                response = requests.post(url, data=data)
                content = response.content

                if isinstance(response.content, str):
                    try:
                        content = json.loads(response.content)
                    except ValueError:
                        content = parse_qs(response.content)
                else:
                    return content


        In my app, I call authorize_url() to get the code.

        @app.route("/authenticate")
        def authenticate():
            auth_url = auth_app.authorize_url()
            return redirect(auth_url)


        After, I get the code by callback url and I call the get_token() method:

        @app.route('/auth', methods=['GET', 'POST'])
        def auth():
            error = request.args.get('error', '')
            if error:
                return "Error: " + error

            code = request.args.get('code')
            content = auth_app.get_token()
            return render_template('index.html', content="content: " + content)


        Github Project: https://github.com/I-am-Gabi/security-app/tree/master/2-BasicAuthentication/securityapp-ui/web

        OAuth2 class: https://github.com/I-am-Gabi/security-app/blob/master/2-BasicAuthentication/securityapp-ui/web/oauth_fiware.py

        App: https://github.com/I-am-Gabi/security-app/blob/master/2-BasicAuthentication/securityapp-ui/web/app.py

        Fiware wiki: https://github.com/ging/fiware-idm/wiki/using-the-fiware-lab-instance
        Created question in FIWARE Q/A platform on 02-09-2016 at 16:09
        {color: red}Please, ANSWER this question AT{color} https://stackoverflow.com/questions/39295231/fiware-authentication-in-python


        +Question:+
        FIWARE Authentication in Python

        +Description:+
        I am trying to authenticate user using FIWARE. It returns a 404 when I request the token, but I don't have problems to get access code request.
        My code:

        class OAuth2(object):
            def __init__(self):
                self.client_id = "&lt;client_id&gt;"
                self.client_secret = "&lt;client_secret&gt;"
                self.site = 'http://0.0.0.0:8000&#39;
                self.redirect_uri = "http://192.168.99.101:8000/auth"
                self.authorization_url = '/oauth2/authorize'
                self.token_url = '/oauth2/token'

            def authorize_url(self, **kwargs):
                oauth_params = {'response_type': 'code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id}
                oauth_params.update(kwargs)
                return "%s%s?%s" % (self.site, quote(self.authorization_url), urlencode(oauth_params))

            def get_token(self, code, **kwargs):
                url = "%s%s" % (self.site, quote(self.token_url))
                data = {'grant_type': 'authorization_code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code}
                data.update(kwargs)
                response = requests.post(url, data=data)
                content = response.content

                if isinstance(response.content, str):
                    try:
                        content = json.loads(response.content)
                    except ValueError:
                        content = parse_qs(response.content)
                else:
                    return content


        In my app, I call authorize_url() to get the code.

        @app.route("/authenticate")
        def authenticate():
            auth_url = auth_app.authorize_url()
            return redirect(auth_url)


        After, I get the code by callback url and I call the get_token() method:

        @app.route('/auth', methods=['GET', 'POST'])
        def auth():
            error = request.args.get('error', '')
            if error:
                return "Error: " + error

            code = request.args.get('code')
            content = auth_app.get_token()
            return render_template('index.html', content="content: " + content)


        Github Project: https://github.com/I-am-Gabi/security-app/tree/master/2-BasicAuthentication/securityapp-ui/web

        OAuth2 class: https://github.com/I-am-Gabi/security-app/blob/master/2-BasicAuthentication/securityapp-ui/web/oauth_fiware.py

        App: https://github.com/I-am-Gabi/security-app/blob/master/2-BasicAuthentication/securityapp-ui/web/app.py

        Fiware wiki: https://github.com/ging/fiware-idm/wiki/using-the-fiware-lab-instance
        fla Fernando Lopez made changes -
        Assignee Backlog Manager [ backlogmanager ]
        fla Fernando Lopez made changes -
        Fix Version/s 2021 [ 12600 ]

          People

          • Assignee:
            backlogmanager Backlog Manager
            Reporter:
            backlogmanager Backlog Manager
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: