Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View rishavs's full-sized avatar
💭
Its the end of the world as we know it, but I'm feeling fine

Rishav Sharan rishavs

💭
Its the end of the world as we know it, but I'm feeling fine
View GitHub Profile
@rishavs
rishavs / csrf.cr
Created October 18, 2020 20:01
CSRF protection in Crystal
# Embed this as a <input type="hidden"> into *every form*
def csrf_token(session_id, key = ENV_CSRF_TOKEN_SECRET)
d = OpenSSL::Digest.new("SHA256")
d.update session_id.to_slice
payload = String.build do |s|
s << d.hexdigest
s << " "
s << Time.utc.to_unix
end
"#{OpenSSL::HMAC.hexdigest(CSRF_TOKEN_HMAC_ALGO, key, payload)} #{payload}"
@rishavs
rishavs / Main.hx
Created January 16, 2020 18:40
Stuck in creating basic scene manager for haxe + openfl
package;
import openfl.text.TextField;
import openfl.events.KeyboardEvent;
import openfl.display.Sprite;
class Scene extends Sprite {
public function new()
{
super();
module Cove::Views
def self.welcome (store)
html = <<-HTML
<h1>Hello #{store["data"]["username"]}</h1>
<p> Your UserId is: #{store["data"]["userid"]}</p>
HTML
end
end
module Cove
class Auth
def self.register(ctx)
params = Cove::Parse.form_params(ctx.request.body)
username = params.fetch("username")
password = params.fetch("password")
store = {
"status" => "success",
"message" => "User was success fully added",
module Cove::Views
def self.register
html = <<-HTML
<article id="register_page">
<form class="ui form" id="register_form" action="/register" method="post">
<div class="field">
<label>Username</label>
<input type="text" name="username" id="username" placeholder="Username" />
</div>
<div class="field">
def self.run(method, url, ctx)
url = clean(url)
path = {url, method}
case path
when {"/hola/", "GET"}
ctx.response.print "Yo buddy!"
when {"/about/", "GET"}
ctx.response.content_type = "text/html; charset=utf-8"
# A handler that logs the request method, resource, status code, and
# the time used to execute the next handler, to the given `IO`.
class Cove::Logger < HTTP::LogHandler
# Initializes this handler to log to the given `IO`.
def initialize(@io : IO = STDOUT)
end
def call(context)
elapsed = Time.measure { call_next(context) }
module Cove::Views
def self.js
html = <<-HTML
<script>
console.log("Hallo from the Cove")
</script>
HTML
end
end
module Cove::Views
def self.css
html = <<-HTML
<style type="text/css">
.main.container {
margin-top: 6em;
}
</style>
HTML
end
module Cove::Views
def self.navbar
html = <<-HTML
<div class="ui fixed menu">
<div class="ui container">
<a href="/" class="header item" >
Digglu
</a>
<div class="item">
<a href="/about" >About</a>