-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitHub authentication and profile caching
- Loading branch information
Showing
10 changed files
with
228 additions
and
21 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,12 +1,11 @@ | ||
structure O = Oauth.Make(struct | ||
structure G = Github.Make(struct | ||
open Basic_in | ||
|
||
val authorize_url = bless "https://github.com/login/oauth/authorize" | ||
val access_token_url = bless "https://github.com/login/oauth/access_token" | ||
val https = False | ||
end) | ||
|
||
val after = | ||
return <xml>Welcome back.</xml> | ||
u <- G.whoami; | ||
return <xml>Welcome back, {[u]}.</xml> | ||
|
||
fun main () = | ||
O.authorize {ReturnTo = url after} | ||
G.authorize {ReturnTo = url after} |
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,3 +1,4 @@ | ||
#include <urweb.h> | ||
|
||
uw_Basis_string uw_WorldFfi_post(uw_context ctx, uw_Basis_string url, uw_Basis_string body); | ||
uw_Basis_string uw_WorldFfi_get(uw_context ctx, uw_Basis_string url, uw_Basis_string auth); |
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,112 @@ | ||
open Json | ||
|
||
table users : { Login : string, | ||
AvatarUrl : string, | ||
Nam : option string, | ||
Company : option string, | ||
Blog : option string, | ||
Location : option string, | ||
Email : option string, | ||
Hireable : option bool, | ||
Bio : option string } | ||
PRIMARY KEY Login | ||
|
||
con users_hidden_constraints = _ | ||
constraint [Pkey = [Login]] ~ users_hidden_constraints | ||
|
||
table secrets : { Login : string, | ||
Secret : int } | ||
PRIMARY KEY Login, | ||
CONSTRAINT Login FOREIGN KEY Login REFERENCES users(Login) ON DELETE CASCADE | ||
|
||
cookie user : { Login : string, Secret : int } | ||
|
||
signature S = sig | ||
val client_id : string | ||
val client_secret : string | ||
val https : bool | ||
end | ||
|
||
type profile = { Login : string, | ||
AvatarUrl : string, | ||
Nam : option string, | ||
Company : option string, | ||
Blog : option string, | ||
Location : option string, | ||
Email : option string, | ||
Hireable : option bool, | ||
Bio : option string } | ||
|
||
val json_profile : json profile = | ||
json_record {Login = "login", | ||
AvatarUrl = "avatar_url", | ||
Nam = "name", | ||
Company = "company", | ||
Blog = "blog", | ||
Location = "location", | ||
Email = "email", | ||
Hireable = "hireable", | ||
Bio = "bio"} | ||
|
||
functor Make(M : S) = struct | ||
open M | ||
|
||
fun withToken tok = | ||
profile <- WorldFfi.get (bless "https://api.github.com/user") (Some tok); | ||
debug profile; | ||
(profile : profile) <- return (Json.fromJson profile); | ||
exists <- oneRowE1 (SELECT COUNT( * ) > 0 | ||
FROM users | ||
WHERE users.Login = {[profile.Login]}); | ||
secret <- | ||
(if exists then | ||
dml (UPDATE users | ||
SET AvatarUrl = {[profile.AvatarUrl]}, | ||
Nam = {[profile.Nam]}, | ||
Company = {[profile.Company]}, | ||
Blog = {[profile.Blog]}, | ||
Location = {[profile.Location]}, | ||
Email = {[profile.Email]}, | ||
Hireable = {[profile.Hireable]}, | ||
Bio = {[profile.Bio]} | ||
WHERE Login = {[profile.Login]}); | ||
oneRowE1 (SELECT (secrets.Secret) | ||
FROM secrets | ||
WHERE secrets.Login = {[profile.Login]}) | ||
else | ||
dml (INSERT INTO users(Login, AvatarUrl, Nam, Company, Blog, Location, | ||
Email, Hireable, Bio) | ||
VALUES ({[profile.Login]}, {[profile.AvatarUrl]}, {[profile.Nam]}, | ||
{[profile.Company]}, {[profile.Blog]}, {[profile.Location]}, | ||
{[profile.Email]}, {[profile.Hireable]}, {[profile.Bio]})); | ||
secret <- rand; | ||
dml (INSERT INTO secrets(Login, Secret) | ||
VALUES ({[profile.Login]}, {[secret]})); | ||
return secret); | ||
|
||
setCookie user {Value = {Login = profile.Login, Secret = secret}, | ||
Expires = None, | ||
Secure = https} | ||
|
||
open Oauth.Make(struct | ||
open M | ||
|
||
val authorize_url = bless "https://github.com/login/oauth/authorize" | ||
val access_token_url = bless "https://github.com/login/oauth/access_token" | ||
val withToken = withToken | ||
end) | ||
|
||
val whoami = | ||
c <- getCookie user; | ||
case c of | ||
None => return None | ||
| Some r => | ||
ok <- oneRowE1 (SELECT COUNT( * ) > 0 | ||
FROM secrets | ||
WHERE secrets.Login = {[r.Login]} | ||
AND secrets.Secret = {[r.Secret]}); | ||
if ok then | ||
return (Some r.Login) | ||
else | ||
error <xml>Invalid login information</xml> | ||
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,21 @@ | ||
table users : { Login : string, | ||
AvatarUrl : string, | ||
Nam : option string, | ||
Company : option string, | ||
Blog : option string, | ||
Location : option string, | ||
Email : option string, | ||
Hireable : option bool, | ||
Bio : option string } | ||
PRIMARY KEY Login | ||
|
||
signature S = sig | ||
val client_id : string | ||
val client_secret : string | ||
val https : bool | ||
end | ||
|
||
functor Make(M : S) : sig | ||
val authorize : { ReturnTo : url } -> transaction page | ||
val whoami : transaction (option string) | ||
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 |
---|---|---|
|
@@ -5,5 +5,7 @@ effectful WorldFfi.post | |
|
||
$/char | ||
$/string | ||
$/json | ||
urls | ||
oauth | ||
github |
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 +1,2 @@ | ||
val post : url -> string -> transaction string | ||
val get : url -> option string -> transaction string |