Giter VIP home page Giter VIP logo

ucfopen / lti-template-flask-oauth-tokens Goto Github PK

View Code? Open in Web Editor NEW
14.0 9.0 8.0 56 KB

LTI template written in Python using the Flask framework. Uses Instructure's oAuth login to generate an API Key. Quickly create a new application that can be launched through your Learning Management System using the LTI protocol.

License: MIT License

Python 99.35% CSS 0.14% JavaScript 0.11% Shell 0.40%
lti lti-tool python flask canvas-lms

lti-template-flask-oauth-tokens's Introduction

Join UCF Open Slack Discussions

LTI Template: Flask with OAUTH tokens

Set up a development server

Virtual Environment

  • Create a virtual environment and install from requirements.txt.
virtualenv env
source env/bin/activate
pip install -r requirements.txt

Create developer key

Before running the testing server, you will need to create an OAUTH key and secret. On an Instructure-hosted Canvas instance, enter your Account Admin page and click Developer Keys. Click + Developer Key to create a new key.

In the dialog that appears, give your key a name and set the owner email to your own email address. Enter https://[my_server_url]/oauthlogin as a redirect URI. Click "Save Key" when you are satisfied with all of the fields.

You will see your new key created in Canvas. Toggle its state from "Off" to "On" so your key is ready to use.

Scope enforcement

You may optionally enable scope enforcement when you create your developer key for Canvas. This means that people using keys derived from your developer key will only be able to access the endpoints which are listed in the enforcement list, which is good for security.

To do this, turn on Enforce Scopes during the creation of your key. You can find the scopes you need beneath each API endpoint in the Canvas API documentation, listed after Scope:. The format listed is the exact format you'll need to use for our settings file later.

The base template requires that url:GET|/api/v1/users/:user_id/profile is checked. Your use of the Canvas API may require more.

Add server config

Copy settings.py.template to settings.py in this project's directory. Open it to configure the following options:

  1. Set BASE_URL to the URL for your Canvas instance, for example https://institution.instructure.com/. Be sure to add the trailing slash!
  2. Set API_URL to the URL for the API of your Canvas instance. In most cases, this is the same as BASE_URL. Be sure to add the trailing slash!
  3. Set LTI_CONSUMER_KEY and LTI_SHARED_SECRET to any value you wish. Save this information for later, we'll use it while setting up the LTI.
  4. Enter a random string in the secret_key field. This secret key is used to sign session cookies to prevent an attacker from tampering with their contents.
  5. Set oauth2_id and oauth2_key to the ID and Key that Canvas gives you, respectively. The ID is shown under the "Details" column on the Developer Keys page. The key can be shown by clicking the "Show Key" button under the ID.
  6. Set oauth2_uri to the redirect URI you entered for the developer key earlier.

You may also set oauth2_scopes if you wish. It takes a list of scope values, one for each API endpoint you will be using with the access tokens you gain. If you have scope enforcement enabled on your developer key, you must set oauth2_scopes to a list of endpoints less than or equal to those granted to your developer key.

If you are using canvasapi, the endpoint used for each call is listed in its class reference documentation. You may click on the endpoint listing to learn the Scope: value for the endpoint.

Create a DB

  • Modify the model as you see fit before creating the db! SQLAlchemy can make migrating a pain.
  • Here's how to use the example code:
  • Change directory into the project folder. Create the database in python shell:
    from views import db
    db.create_all()
  • If you want to look at your users table in the future, you can look at it in the python shell:
    from views import Users
    Users.query.all()

Run

Here's how you run the flask app from the terminal:

export FLASK_APP=views.py
flask run

Install LTI

  • Have the XML, consumer key, and secret ready.
  • Navigate to the course that you would like the LTI to be added to. Click Settings in the course navigation bar. Then, select the Apps tab. Near the tabs on the right side, click 'View App Configurations'. It should lead to a page that lists what LTIs are inside the course. Click the button near the tabs that reads '+ App'.
  • A modal should come up that allows you to customize how the app gets added. Change the configuration in the Configuration Type dropdown menu to 'By URL' or 'Paste XML' depending on how you have your LTI configured. If your LTI is publicly accessible, 'By URL' is recommended. From there, fill out the Name and Consumer Keys, and the Config URL or XML Configuration. Click Submit.
  • Your LTI will appear depending on specifications in the XML. Currently, they get specified in the options tag within the extensions tag. Extensions can include these options:
    • Editor Button (visible from within any wiki page editor in Canvas)
    • Homework Submission (when a student is submitting content for an assignment)
    • Course Navigation (link on the lefthand nav)
    • Account Navigation (account-level navigation)
    • User Navigation (user profile)

Note: If you're using Canvas, your version might be finicky about SSL certificates. Keep HTTP/HTTPS in mind when creating your XML and while developing your project. Some browsers will disable non-SSL LTI content until you enable it through clicking a shield in the browser bar or something similar.

lti-template-flask-oauth-tokens's People

Contributors

ctcuff avatar ssilverm avatar thetwam avatar universalsuperbox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lti-template-flask-oauth-tokens's Issues

Switching Browser ReAuth

Summary

When switching browsers, the tool asks a user to re-authorize itself.

Steps to Reproduce

  1. Log into a Canvas Course and launch the tool
  2. Open a different browser and go to the same course and launch the tool
  3. Observe new request to authorize the tool w/ Canvas

Expected Behavior

The tool should use the refresh token already in it's DB to authenticate the user, and should not ask to authorize.

Observed Behavior

The tool asks user to re-authorize itself with Canvas. This creates additional tokens each time a user switches browsers.

Template does not handle "Unauthorized token" error

When using a developer key that isn't turned on or is invalid, the template sends you to OAUTH, then Canvas returns you with an error in the URL string: ?error=unauthorized_client&error_description=Client+does+not+have+access+to+the+specified+Canvas+account.. The template then tries to sign in using a code attribute from the URL that doesn't exist.

We should handle cases where we don't have a code and inform the developer that they've done something wrong.

pip 21.0 does not support git format in requirements.txt

Running "pip install -r requirements.txt" where pip version >= 21.0 gives the following error:

ERROR: [email protected]:ucfcdl/pylti.git@roles#egg=PyLTI is not a valid editable requirement. It should either be a path to a local project or a VCS URL (beginning with bzr+http, bzr+https, bzr+ssh, bzr+sftp, bzr+ftp, bzr+lp, bzr+file, git+http, git+https, git+ssh, git+git, git+file, hg+file, hg+http, hg+https, hg+ssh, hg+static-http, svn+ssh, svn+http, svn+https, svn+svn, svn+file).

pip doesn't support the git format above as of 21.0, see: https://pip.pypa.io/en/stable/reference/pip_install/#git

Fix for me was to change this line in requirements.txt from:

-e [email protected]:ucfcdl/pylti.git@roles#egg=PyLTI

to:

-e git+https://github.com/ucfcdl/pylti.git@roles#egg=PyLTI

I can submit a PR for this if you like.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.