This repository has been archived by the owner on Jul 5, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
class ApplicationController < ActionController::API | ||
include ActionController::RequestForgeryProtection | ||
protect_from_forgery with: :exception | ||
after_action :set_csrf_token_header | ||
|
||
def set_csrf_token_header | ||
response.set_header("X-CSRF-Token", form_authenticity_token) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class TopController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
def get | ||
head :no_content | ||
end | ||
|
||
def post | ||
render json: {text: params[:top][:text]} | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class Users::SessionsController < Devise::SessionsController | ||
skip_before_action :verify_authenticity_token, only: :create | ||
end |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Rails.application.routes.draw do | ||
devise_for :users | ||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
devise_for :users, controllers: {sessions: 'users/sessions'} | ||
get '/get', to: 'top#get' | ||
post '/post', to: 'top#post' | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>CSRF protection demo</title> | ||
<script src="https://unpkg.com/vue@2.5.21/dist/vue.js"></script> | ||
<script src="https://unpkg.com/axios@0.18.0/dist/axios.js"></script> | ||
</head> | ||
<body> | ||
<div id="app"> | ||
<div><button v-on:click="login">Login</button></div> | ||
|
||
<div><button v-on:click="get">GET</button></div> | ||
|
||
<div> | ||
<form v-on:submit.prevent="post"> | ||
<input type="text" v-model="text" /> | ||
<button type="submit">POST</button> | ||
</form> | ||
<p>Response: {{ response }}</p> | ||
</div> | ||
|
||
<div> | ||
<p>CSRF Token: {{ csrfToken }}</p> | ||
<button type="submit" v-on:click="clearToken">clear Token</button> | ||
</div> | ||
</div> | ||
|
||
<script> | ||
var http = axios.create(); | ||
http.interceptors.response.use(function (response) { | ||
var token = response.headers['x-csrf-token']; | ||
if (token) { | ||
// save token in localStorage for later use | ||
app.csrfToken = token; | ||
window.localStorage.setItem('csrf-token', token); | ||
} | ||
return response; | ||
}, function (error) {}); | ||
http.interceptors.request.use(function (config) { | ||
var token = window.localStorage.getItem('csrf-token'); | ||
config.headers['X-CSRF-Token'] = token; | ||
return config; | ||
}, function (error) {}); | ||
|
||
var app = new Vue({ | ||
el: '#app', | ||
data: { | ||
csrfToken: window.localStorage.getItem('csrf-token'), | ||
text: '', | ||
response: '-' | ||
}, | ||
methods: { | ||
login: function () { | ||
http.post('/users/sign_in', { | ||
user: { | ||
email: 'username@example.com', | ||
password: 'password' | ||
} | ||
}, {}); | ||
}, | ||
get: function () { | ||
http.get('/get'); | ||
}, | ||
post: function () { | ||
http.post('/post', { | ||
top: {text: this.text} | ||
}, {}).then(function (response) { | ||
app.response = response.data.text; | ||
}); | ||
}, | ||
clearToken: function () { | ||
window.localStorage.removeItem('csrf-token'); | ||
this.csrfToken = '-'; | ||
} | ||
} | ||
}); | ||
</script> | ||
</body> | ||
</html> |