Last active
August 29, 2015 14:09
-
-
Save anoxic/2e6d8042dc3e4cc99afe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ANATOMY OF A JWT (pronounced 'jot') | |
<encoded header>.<encoded payload>.<signature> | |
Header Base64url-encoded JSON of 'alg' (algorithm) and 'typ' (type) | |
Payload Base64url-encoded JSON of claims to be verified | |
Signature "<Header>.<Payload>" encrypted according to 'alg' in the Header | |
Two useful payload fields: 'iat' (issued at), and 'exp' (expiration). | |
-- | |
One use: authentication, instead of a cookie. | |
A JWT will be included in the 'Authentication' header | |
of every request. JWT generation is made through a login api, | |
which takes an email and password, and returns a token. | |
-- | |
Possibly useful links | |
JSON Web Tokens, OWIN, and AngularJS (http://mikehadlow.blogspot.com/2014/04/json-web-tokens-owin-and-angularjs.html) | |
JSON Web Tokens (http://jwt.io) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (run $(mix phoenix.start) at any time to test your code) | |
# (run $(mix phoenix.routes) to see all of your routes) | |
#### | |
# Set up a Phoenix project | |
#$ cd $PHOENIX_DIR | |
#$ mix phoenix.new project_name ~/code/project_name | |
#$ cd ~/code/project_name | |
#$ mix do deps.get, compile | |
#$ mix phoenix.start | |
#$ echo "mix.lock" >> .gitignore | |
#$ git add . | |
#$ git commit -m "Phoenix default project files" | |
# --- | |
# Change dev port to 8000 | |
#$ sed "s/4000/8000/" config/dev.exs -i | |
#$ git commit config -m "Change dev port to 8000" | |
# Add Postgrex and Ecto to deps in mix.exs | |
{:postgrex, ">= 0.6.0"}, | |
{:ecto, "~> 0.2.5"} | |
#$ git commit mix.exs -m "Add Postgrex and Ecto Dependencies" | |
# | |
#################### | |
# | |
# @todo Explain how to set up a Postgres user with a password and login access | |
# | |
#################### | |
# | |
# Add a repo (Ecto DB wrapper - see Ecto notes) | |
# lib/repo.ex | |
defmodule Repo do | |
use Ecto.Repo, adapter: Ecto.Adapters.Postgres | |
def conf do | |
parse_url "postgres://campbell:campbell@localhost/campbell" | |
end | |
def priv do | |
app_dir(:crud, "priv/repo") | |
end | |
end | |
# Add a model | |
# lib/user.ex | |
defmodule User do | |
use Ecto.Model | |
schema "user" do | |
field :gen, :datetime # datetime of user creation | |
field :pw # a hash of the password + gentime | |
field :name # user's name | |
field :bio # a bio for the user | |
field :email # the user's email address | |
end | |
end | |
# add the repo to our supervisor chain (in lib/crud.ex) | |
children = [ | |
worker(Repo, []) | |
] | |
# create a migration | |
#$ mix ecto.gen.migration Repo create_users | |
# manually add the postgres schema to priv/repo/migrations/XXXX_create_users.ex | |
defmodule Repo.Migrations.CreateUsers do | |
use Ecto.Migration | |
def up do | |
"CREATE TABLE IF NOT EXISTS users( | |
id serial primary key, | |
gen timestamp not null default(now() at time zone 'utc'), | |
pw varchar, | |
name varchar, | |
bio varchar, | |
email varchar | |
)" | |
end | |
def down do | |
"DROP TABLE users" | |
end | |
end | |
# run the migration | |
#$ mix ecto.migrate Repo # you can rollback with $(mix ecto.rollback Repo) | |
# ROUTES, CONTROLLERS, and TEMPLATES | |
# Each url slug you want to define needs to have | |
# a route, controller, view, and (if applicable) template | |
# Routes: | |
# Matches standard GET/POST actions | |
# Must have a pipe defined (:browser or :api) | |
# When matched call a Controller | |
# for this route (in web/router.ex) | |
defmodule Campbell.Router do | |
use Phoenix.Router | |
scope "/" do | |
pipe_through :browser | |
get "/", Campbell.HomeController, :index | |
end | |
end | |
# the controller will be stored in web/controllers/home_controller.ex | |
defmodule Crud.HomeController do | |
use Phoenix.Controller | |
plug :action | |
def index(conn, _params) do | |
render conn, "index" | |
end | |
end | |
# and in web/views/home_view.ex | |
defmodule Crud.HomeView do | |
use Crud.Views | |
end | |
# web/templates/home/index.html.eex | |
<p>This is a test!</p> | |
# If everything worked, you can start Pheonix with $(mix phoenix.start) | |
# and view from port 8000. | |
# with this, you will be calling the "index" template for Home | |
# kept in web/templates/home/index.html.eex | |
# this template will actually be rendered within <%= @inner %> in | |
# web/templates/layout/application.html.eex | |
# add a page controller | |
# add to web/router.ex | |
get "/pages/:page", Crud.PageController, :show, as: :page | |
# web/controllers/page_controller.ex | |
defmodule Crud.PageController do | |
use Phoenix.Controller | |
plug :action | |
def show(conn, %{"page" => "unauthorized"}) do | |
conn | |
|> assign_layout(:none) | |
|> render "unauthorized" | |
end | |
def show(conn, %{"page" => page}) do | |
render conn, "show", page: page | |
end | |
end | |
# web/templates/page/unauthorized.html.eex | |
<h1>Not Authorized!</h1> | |
# web/templates/page/show.html.eex | |
<h1>Welcome to <%= @name %>!</h1> | |
# --- | |
# FOLDER STRUCTURE | |
# _build/ ? | |
# config/ config files | |
# deps/ dependencies - managed by mix | |
# lib/ where we keep our own library modules | |
# mix.exs like a Gemfile, defines the project and dependencies | |
# priv ? | |
# test test cases | |
# web - models MVC models | |
# - views view "modules", which allows us to include helpers for the templates | |
# - controllers controllers which relate to our routes | |
# - templates templates for the web, templates end in .eex | |
# - router.ex where we define routes | |
# --- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment