Skip to content

Instantly share code, notes, and snippets.

@kartoch
Last active April 12, 2018 12:22
Show Gist options
  • Select an option

  • Save kartoch/8891061 to your computer and use it in GitHub Desktop.

Select an option

Save kartoch/8891061 to your computer and use it in GitHub Desktop.

Revisions

  1. kartoch revised this gist Nov 21, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion hubic_credentials.py
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,8 @@
    users fill the hubic authentication form with its navigator and obtain the
    credentials returned by the application.
    You need requests-oauthlib to works:
    You need requests-oauthlib and flask:
    pip install flask
    pip install requests-oauthlib
    The client and id secret are valid but users can create their own application
  2. kartoch revised this gist Nov 21, 2014. 1 changed file with 43 additions and 66 deletions.
    109 changes: 43 additions & 66 deletions hubic_credentials.py
    Original file line number Diff line number Diff line change
    @@ -1,81 +1,58 @@
    #!/usr/bin/env python2
    """
    As Hubic web services are deprecated, this is a small script to request
    access and refresh token. It starts a flask server at http://localhost:5000/, the
    users fill the hubic authentication form with its navigator and obtain the
    credentials returned by the application.
    import base64
    import json
    import logging
    import requests
    import sys
    from swiftclient import client
    You need requests-oauthlib to works:
    pip install requests-oauthlib
    class AuthenticationHubic:
    def __init__(self, user, passwd):
    self.logger = logging.getLogger(__name__)
    self.SessionHandler = 'https://ws.ovh.com/sessionHandler/r4/'
    self.hubicws = 'https://ws.ovh.com/hubic/r5/'
    The client and id secret are valid but users can create their own application
    on hubic and change the values of these entries.
    """

    r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'getAnonymousSession')
    sessionId = r.json()['answer']['session']['id']
    from requests_oauthlib import OAuth2Session
    from flask import Flask, request, redirect, session, url_for
    from flask.json import jsonify
    import os
    import uuid;

    self.logger.info("sessionId:" + sessionId)
    os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

    params = { 'sessionId': sessionId,
    'email': user}
    payload = {'params': json.dumps(params)}
    app = Flask(__name__)

    r = requests.get(self.hubicws + 'rest.dispatcher/' + 'getHubics',
    params=payload)
    hubics = r.json()
    self.hubicsId = hubics['answer'][0]['id']
    client_id = 'api_hubic_dMCUY8MLynmkpIgdrj2D8GlbaSPemHAE'
    client_secret = '9TgfdUFvjTCXgOahwz0ai1kEh03RrvDKI0kw4kZ3hCCbC2pQPtvJSdcKlTyFz3Q6'

    self.logger.info("hubicsId:" + self.hubicsId)
    authorization_base_url = 'https://api.hubic.com/oauth/auth/?'
    token_url = 'https://api.hubic.com/oauth/token/'
    redirect_uri = "http://localhost:5000/callback"

    params = { 'login': hubics['answer'][0]['nic'],
    'password': passwd,
    'context': 'hubic'}
    payload = {'params': json.dumps(params)}
    scope = u"usage.r,account.r,getAllLinks.r,credentials.r,activate.w,links.drw"

    r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'login',
    params=payload)
    state = 'RandomString_' + str(uuid.uuid4().get_hex().upper()[0:10])

    self.sessionId = r.json()['answer']['session']['id']
    @app.route("/")
    def start():
    hubic = OAuth2Session(client_id, scope=scope, state=state, redirect_uri=redirect_uri)
    authorization_url, state_back = hubic.authorization_url(authorization_base_url)
    return redirect(authorization_url)

    self.logger.info("authenticated hubicsId:" + self.sessionId)
    @app.route("/callback", methods=["GET"])
    def callback():
    hubic = OAuth2Session(client_id, state=state,redirect_uri=redirect_uri)
    token = hubic.fetch_token(token_url, client_secret=client_secret,
    authorization_response=request.url)
    session['oauth_token'] = token
    return redirect(url_for('.account'))

    def get_credentials(self):
    params = { 'sessionId': self.sessionId,
    'hubicId': self.hubicsId}
    payload = {'params': json.dumps(params)}

    r = requests.get(self.hubicws + 'rest.dispatcher/' + 'getHubic',
    params=payload)
    Storage_Url = base64.b64decode(r.json()['answer']['credentials']['username'])
    @app.route("/account", methods=["GET"])
    def account():
    hubic = OAuth2Session(client_id, token=session['oauth_token'])
    return jsonify(session['oauth_token'])

    self.logger.info("storage url:" + str(Storage_Url))

    Auth_Token = r.json()['answer']['credentials']['secret']

    self.logger.info("auth token:" + Auth_Token)

    return Storage_Url, Auth_Token

    def logout(self):
    params = { 'sessionId': self.sessionId}
    payload = {'params': json.dumps(params)}
    r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'logout',
    params=payload)


    if __name__ == '__main__':

    logging.basicConfig(level = logging.ERROR)

    if len(sys.argv) != 3:
    logging.error("Usage: sys.argv[0] login password")
    sys.exit(1)
    login = sys.argv[1]
    passwd = sys.argv[2]
    auth = AuthenticationHubic(login, passwd)
    storage_url, auth_token = auth.get_credentials()
    print("export SWIFT_PREAUTHURL=\"" + str(storage_url) +"\";" )
    print("export SWIFT_PREAUTHTOKEN=\"" + auth_token + "\"")
    auth.logout()
    if __name__ == "__main__":
    app.secret_key = os.urandom(24)
    app.run(debug=True)
  3. kartoch created this gist Feb 8, 2014.
    81 changes: 81 additions & 0 deletions hubic_credentials.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    #!/usr/bin/env python2

    import base64
    import json
    import logging
    import requests
    import sys
    from swiftclient import client

    class AuthenticationHubic:
    def __init__(self, user, passwd):
    self.logger = logging.getLogger(__name__)
    self.SessionHandler = 'https://ws.ovh.com/sessionHandler/r4/'
    self.hubicws = 'https://ws.ovh.com/hubic/r5/'

    r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'getAnonymousSession')
    sessionId = r.json()['answer']['session']['id']

    self.logger.info("sessionId:" + sessionId)

    params = { 'sessionId': sessionId,
    'email': user}
    payload = {'params': json.dumps(params)}

    r = requests.get(self.hubicws + 'rest.dispatcher/' + 'getHubics',
    params=payload)
    hubics = r.json()
    self.hubicsId = hubics['answer'][0]['id']

    self.logger.info("hubicsId:" + self.hubicsId)

    params = { 'login': hubics['answer'][0]['nic'],
    'password': passwd,
    'context': 'hubic'}
    payload = {'params': json.dumps(params)}

    r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'login',
    params=payload)

    self.sessionId = r.json()['answer']['session']['id']

    self.logger.info("authenticated hubicsId:" + self.sessionId)

    def get_credentials(self):
    params = { 'sessionId': self.sessionId,
    'hubicId': self.hubicsId}
    payload = {'params': json.dumps(params)}

    r = requests.get(self.hubicws + 'rest.dispatcher/' + 'getHubic',
    params=payload)
    Storage_Url = base64.b64decode(r.json()['answer']['credentials']['username'])

    self.logger.info("storage url:" + str(Storage_Url))

    Auth_Token = r.json()['answer']['credentials']['secret']

    self.logger.info("auth token:" + Auth_Token)

    return Storage_Url, Auth_Token

    def logout(self):
    params = { 'sessionId': self.sessionId}
    payload = {'params': json.dumps(params)}
    r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'logout',
    params=payload)


    if __name__ == '__main__':

    logging.basicConfig(level = logging.ERROR)

    if len(sys.argv) != 3:
    logging.error("Usage: sys.argv[0] login password")
    sys.exit(1)
    login = sys.argv[1]
    passwd = sys.argv[2]
    auth = AuthenticationHubic(login, passwd)
    storage_url, auth_token = auth.get_credentials()
    print("export SWIFT_PREAUTHURL=\"" + str(storage_url) +"\";" )
    print("export SWIFT_PREAUTHTOKEN=\"" + auth_token + "\"")
    auth.logout()