login!
This commit is contained in:
parent
a62b7d1b5a
commit
d93fe67216
7 changed files with 154 additions and 2 deletions
|
@ -14,6 +14,11 @@ version = "1.0.0"
|
|||
|
||||
[dependencies]
|
||||
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
|
||||
mist = ">= 4.0.7 and < 5.0.0"
|
||||
wisp = ">= 1.6.0 and < 2.0.0"
|
||||
gleam_erlang = ">= 0.34.0 and < 1.0.0"
|
||||
envoy = ">= 1.0.2 and < 2.0.0"
|
||||
gleam_http = ">= 4.0.0 and < 5.0.0"
|
||||
|
||||
[dev-dependencies]
|
||||
gleeunit = ">= 1.0.0 and < 2.0.0"
|
||||
|
|
|
@ -2,10 +2,35 @@
|
|||
# You typically do not need to edit this file
|
||||
|
||||
packages = [
|
||||
{ name = "directories", version = "1.2.0", build_tools = ["gleam"], requirements = ["envoy", "gleam_stdlib", "platform", "simplifile"], otp_app = "directories", source = "hex", outer_checksum = "D13090CFCDF6759B87217E8DDD73A75903A700148A82C1D33799F333E249BF9E" },
|
||||
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
|
||||
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
|
||||
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
|
||||
{ name = "gleam_crypto", version = "1.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "917BC8B87DBD584830E3B389CBCAB140FFE7CB27866D27C6D0FB87A9ECF35602" },
|
||||
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
|
||||
{ name = "gleam_http", version = "4.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "0A62451FC85B98062E0907659D92E6A89F5F3C0FBE4AB8046C99936BF6F91DBC" },
|
||||
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
|
||||
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
|
||||
{ name = "gleam_stdlib", version = "0.59.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "F8FEE9B35797301994B81AF75508CF87C328FE1585558B0FFD188DC2B32EAA95" },
|
||||
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||
{ name = "gleeunit", version = "1.3.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "A7DD6C07B7DA49A6E28796058AA89E651D233B357D5607006D70619CD89DAAAB" },
|
||||
{ name = "glisten", version = "7.0.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "1A53CF9FB3231A93FF7F1BD519A43DC968C1722F126CDD278403A78725FC5189" },
|
||||
{ name = "gramps", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "59194B3980110B403EE6B75330DB82CDE05FC8138491C2EAEACBC7AAEF30B2E8" },
|
||||
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
|
||||
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
|
||||
{ name = "marceau", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "2D1C27504BEF45005F5DFB18591F8610FB4BFA91744878210BDC464412EC44E9" },
|
||||
{ name = "mist", version = "4.0.7", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "gleam_yielder", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "F7D15A1E3232E124C7CE31900253633434E59B34ED0E99F273DEE61CDB573CDD" },
|
||||
{ name = "platform", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "platform", source = "hex", outer_checksum = "8339420A95AD89AAC0F82F4C3DB8DD401041742D6C3F46132A8739F6AEB75391" },
|
||||
{ name = "simplifile", version = "2.2.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "C88E0EE2D509F6D86EB55161D631657675AA7684DAB83822F7E59EB93D9A60E3" },
|
||||
{ name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" },
|
||||
{ name = "wisp", version = "1.6.0", build_tools = ["gleam"], requirements = ["directories", "exception", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_stdlib", "logging", "marceau", "mist", "simplifile"], otp_app = "wisp", source = "hex", outer_checksum = "AE1C568FE30718C358D3B37666DF0A0743ECD96094AD98C9F4921475075F660A" },
|
||||
]
|
||||
|
||||
[requirements]
|
||||
envoy = { version = ">= 1.0.2 and < 2.0.0" }
|
||||
gleam_erlang = { version = ">= 0.34.0 and < 1.0.0" }
|
||||
gleam_http = { version = ">= 4.0.0 and < 5.0.0" }
|
||||
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
|
||||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
|
||||
mist = { version = ">= 4.0.7 and < 5.0.0" }
|
||||
wisp = { version = ">= 1.6.0 and < 2.0.0" }
|
||||
|
|
|
@ -1,5 +1,23 @@
|
|||
import gleam/io
|
||||
import envoy
|
||||
import gleam/erlang/process
|
||||
import gleam/int
|
||||
import mist
|
||||
import switcheroo/router
|
||||
import wisp
|
||||
import wisp/wisp_mist
|
||||
|
||||
pub fn main() -> Nil {
|
||||
io.println("Hello from switcheroo!")
|
||||
wisp.configure_logger()
|
||||
|
||||
let assert Ok(secret_key_base) = envoy.get("SECRET_KEY")
|
||||
let assert Ok(port_str) = envoy.get("PORT")
|
||||
let assert Ok(port) = int.parse(port_str)
|
||||
|
||||
let assert Ok(_) =
|
||||
wisp_mist.handler(router.handle_request, secret_key_base)
|
||||
|> mist.new
|
||||
|> mist.port(port)
|
||||
|> mist.start_http
|
||||
|
||||
process.sleep_forever()
|
||||
}
|
||||
|
|
54
src/switcheroo/login.gleam
Normal file
54
src/switcheroo/login.gleam
Normal file
|
@ -0,0 +1,54 @@
|
|||
import gleam/http.{Delete, Get, Post}
|
||||
import gleam/list
|
||||
import gleam/string_tree
|
||||
import wisp.{type Request, type Response}
|
||||
|
||||
pub const cookie_name = "pluralkit_token"
|
||||
|
||||
pub fn login(req: Request) -> Response {
|
||||
case req.method {
|
||||
Get -> get_login(req)
|
||||
Post -> post_login(req)
|
||||
Delete -> delete_login(req)
|
||||
_ -> wisp.method_not_allowed([Get, Post, Delete])
|
||||
}
|
||||
}
|
||||
|
||||
fn get_login(_req: Request) -> Response {
|
||||
[
|
||||
"<p>Hi, need that one's PluralKit token! <3</p>",
|
||||
"<form action='/login' method='post'>",
|
||||
" <label for='token'><b>PluralKit Token</b></label>",
|
||||
" <input type='string' name='token' id='token' />",
|
||||
" <input type='submit' value='Login'/>", "</form>",
|
||||
]
|
||||
|> string_tree.from_strings
|
||||
|> wisp.html_response(200)
|
||||
}
|
||||
|
||||
fn post_login(req: Request) -> Response {
|
||||
use formdata <- wisp.require_form(req)
|
||||
|
||||
let resp = wisp.redirect("/")
|
||||
case list.key_find(formdata.values, "token") {
|
||||
Ok(token) ->
|
||||
wisp.set_cookie(
|
||||
resp,
|
||||
req,
|
||||
cookie_name,
|
||||
token,
|
||||
wisp.Signed,
|
||||
60 * 60 * 24 * 365,
|
||||
)
|
||||
Error(_) ->
|
||||
wisp.set_header(resp, "x-servfail", "token not found in formdata")
|
||||
}
|
||||
}
|
||||
|
||||
fn delete_login(req: Request) -> Response {
|
||||
let resp = wisp.redirect("/session")
|
||||
case wisp.get_cookie(req, cookie_name, wisp.Signed) {
|
||||
Ok(value) -> wisp.set_cookie(resp, req, cookie_name, value, wisp.Signed, 0)
|
||||
Error(_) -> resp
|
||||
}
|
||||
}
|
7
src/switcheroo/picker.gleam
Normal file
7
src/switcheroo/picker.gleam
Normal file
|
@ -0,0 +1,7 @@
|
|||
import gleam/string_tree
|
||||
import wisp.{type Request, type Response}
|
||||
|
||||
pub fn picker(_req: Request) -> Response {
|
||||
string_tree.from_string("<h1>test</h1>")
|
||||
|> wisp.html_response(200)
|
||||
}
|
31
src/switcheroo/router.gleam
Normal file
31
src/switcheroo/router.gleam
Normal file
|
@ -0,0 +1,31 @@
|
|||
import gleam/http.{Get}
|
||||
import switcheroo/login
|
||||
import switcheroo/picker
|
||||
import switcheroo/web
|
||||
import wisp.{type Request, type Response}
|
||||
|
||||
pub fn handle_request(req: Request) -> Response {
|
||||
use req <- web.middleware(req)
|
||||
|
||||
case wisp.path_segments(req) {
|
||||
[] -> home_page(req)
|
||||
|
||||
["login"] -> login.login(req)
|
||||
["picker"] -> picker.picker(req)
|
||||
|
||||
_ -> wisp.not_found()
|
||||
}
|
||||
}
|
||||
|
||||
fn home_page(req: Request) -> Response {
|
||||
use <- wisp.require_method(req, Get)
|
||||
|
||||
case wisp.get_cookie(req, login.cookie_name, wisp.Signed) {
|
||||
Ok(_) -> {
|
||||
wisp.redirect("/picker")
|
||||
}
|
||||
Error(_) -> {
|
||||
wisp.redirect("/login")
|
||||
}
|
||||
}
|
||||
}
|
12
src/switcheroo/web.gleam
Normal file
12
src/switcheroo/web.gleam
Normal file
|
@ -0,0 +1,12 @@
|
|||
import wisp
|
||||
|
||||
pub fn middleware(
|
||||
req: wisp.Request,
|
||||
handle_request: fn(wisp.Request) -> wisp.Response,
|
||||
) -> wisp.Response {
|
||||
let req = wisp.method_override(req)
|
||||
use <- wisp.log_request(req)
|
||||
use <- wisp.rescue_crashes
|
||||
use req <- wisp.handle_head(req)
|
||||
handle_request(req)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue