Skip to content
This repository has been archived by the owner on Mar 30, 2020. It is now read-only.

Add support for login using Salesforce credentials #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# flask-oauth2-login

Simple OAuth2 Login in Flask
Simple OAuth2 Login in Flask

## License

Expand Down
3 changes: 1 addition & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
app = Flask(__name__)
app.config.update(
SECRET_KEY="secret",
GOOGLE_LOGIN_REDIRECT_SCHEME="http",
)
for config in (
"GOOGLE_LOGIN_CLIENT_ID",
Expand All @@ -31,4 +30,4 @@ def login_failure(e):
return jsonify(error=str(e))

if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
app.run(ssl_context='adhoc', debug=True, host='0.0.0.0', port=8443)
1 change: 1 addition & 0 deletions flask_oauth2_login/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .coursera import CourseraLogin
from .google import GoogleLogin
from .linkedin import LinkedInLogin
from .sfdclogin import SfdcLogin
28 changes: 28 additions & 0 deletions flask_oauth2_login/sfdclogin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from .base import OAuth2Login
import requests


class SfdcLogin(OAuth2Login):
"""Salesforce Oauth2 Login Support."""

config_prefix = "SFDC_"
redirect_endpoint = "_sfdc"
state_session_key = "_sfdc_state"

# default_scope = "api,id,full"
default_scope = "api,id"
default_redirect_path = "/login/salesforce"

auth_url = 'https://login.salesforce.com/services/oauth2/authorize'
token_url = "https://login.salesforce.com/services/oauth2/token"
profile_url = "https://login.salesforce.com/services/oauth2/userinfo"

def get_profile(self, sess):
headers = {
'Authorization': 'Bearer %s' % sess.token["access_token"]
}

resp = requests.get(self.profile_url, headers=headers)
resp.raise_for_status()
return resp.json()

40 changes: 40 additions & 0 deletions sfdc-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os

from flask import Flask, jsonify
from flask_oauth2_login import SfdcLogin
from simple_salesforce import Salesforce

app = Flask(__name__)
app.config.update(
SECRET_KEY="secret",
)

consumer_key = os.environ.get('SFDC_CONNECTED_APP_CONSUMER_KEY')
consumer_secret = os.environ.get('SFDC_CONNECTED_APP_CONSUMER_SECRET')


app.config['SFDC_CLIENT_SECRET'] = consumer_secret
app.config['SFDC_CLIENT_ID'] = consumer_key
sfdc_login = SfdcLogin(app)

@app.route("/")
def index():
return """
<html>
<a href="{}">Login with Salesforce</a>
""".format(sfdc_login.authorization_url())

@sfdc_login.login_success
def login_success(token, profile):
print "Login Success...."
sf = Salesforce(instance_url=token['instance_url'], session_id=token["access_token"])
print "Salesforce API connector %s" % sf
return jsonify(token=token, profile=profile)

@sfdc_login.login_failure
def login_failure(e):
print "Login Failure...."
return jsonify(error=str(e))

if __name__ == '__main__':
app.run(ssl_context='adhoc', debug=True, host='0.0.0.0', port=8443)