-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Credentials to reddit_api_kernel
- Loading branch information
Showing
8 changed files
with
158 additions
and
156 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
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,84 @@ | ||
open! Core | ||
|
||
module Password = struct | ||
type t = | ||
{ client_id : string | ||
; client_secret : string | ||
; password : string | ||
; username : string | ||
} | ||
[@@deriving sexp] | ||
end | ||
|
||
module Refresh_token = struct | ||
type t = | ||
{ client_id : string | ||
; client_secret : string option | ||
; refresh_token : string | ||
} | ||
[@@deriving sexp] | ||
end | ||
|
||
module Userless_confidential = struct | ||
type t = | ||
{ client_id : string | ||
; client_secret : string | ||
} | ||
[@@deriving sexp] | ||
end | ||
|
||
module Userless_public = struct | ||
type t = | ||
{ client_id : string | ||
; device_id : string option | ||
} | ||
[@@deriving sexp] | ||
|
||
let device_id_or_default t = | ||
Option.value t.device_id ~default:"DO_NOT_TRACK_THIS_DEVICE" | ||
;; | ||
end | ||
|
||
type t = | ||
| Password of Password.t | ||
| Refresh_token of Refresh_token.t | ||
| Userless_confidential of Userless_confidential.t | ||
| Userless_public of Userless_public.t | ||
[@@deriving sexp] | ||
|
||
let client_id t = | ||
match t with | ||
| Password { client_id; _ } | ||
| Refresh_token { client_id; _ } | ||
| Userless_confidential { client_id; _ } | ||
| Userless_public { client_id; _ } -> client_id | ||
;; | ||
|
||
let client_secret t = | ||
match t with | ||
| Refresh_token { client_secret = None; _ } | Userless_public _ -> None | ||
| Password { client_secret; _ } | ||
| Refresh_token { client_secret = Some client_secret; _ } | ||
| Userless_confidential { client_secret; _ } -> Some client_secret | ||
;; | ||
|
||
let basic_auth_string t = | ||
let client_id = client_id t in | ||
let client_secret = Option.value (client_secret t) ~default:"" in | ||
Cohttp.Auth.string_of_credential (`Basic (client_id, client_secret)) | ||
;; | ||
|
||
let auth_header t = Cohttp.Header.init_with "Authorization" (basic_auth_string t) | ||
|
||
let access_token_request_params t = | ||
match t with | ||
| Password { username; password; _ } -> | ||
[ "grant_type", [ "password" ]; "username", [ username ]; "password", [ password ] ] | ||
| Refresh_token { refresh_token; _ } -> | ||
[ "grant_type", [ "refresh_token" ]; "refresh_token", [ refresh_token ] ] | ||
| Userless_confidential _ -> [ "grant_type", [ "client_credentials" ] ] | ||
| Userless_public public_credentials -> | ||
[ "grant_type", [ "https://oauth.reddit.com/grants/installed_client" ] | ||
; "device_id", [ Userless_public.device_id_or_default public_credentials ] | ||
] | ||
;; |
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,70 @@ | ||
(** [Password] credentials correspond to Reddit's | ||
{{:https://github.com/reddit-archive/reddit/wiki/oauth2-app-types#script} "script"} | ||
app type. | ||
@see < https://datatracker.ietf.org/doc/html/rfc6749#section-4.3.2 > | ||
The | ||
RFC 6749 section describing the corresponding access token request. *) | ||
module Password : sig | ||
type t = | ||
{ client_id : string | ||
; client_secret : string | ||
; password : string | ||
; username : string | ||
} | ||
[@@deriving sexp] | ||
end | ||
|
||
(** [Refresh_token] credentials correspond to Reddit's | ||
{{:https://github.com/reddit-archive/reddit/wiki/oauth2-app-types#web-app} "web app"} | ||
and | ||
{{:https://github.com/reddit-archive/reddit/wiki/oauth2-app-types#installed-app} "installed-app"} | ||
app types. | ||
@see < https://praw.readthedocs.io/en/stable/tutorials/refresh_token.html | ||
> | ||
{{:https://praw.readthedocs.io/} PRAW}'s documentation on refresh tokens | ||
for advice on obtaining a refresh token, which is currently outside the | ||
scope of this project. | ||
@see < https://datatracker.ietf.org/doc/html/rfc6749#section-6 > | ||
The RFC | ||
6749 section describing the corresponding access token request. *) | ||
module Refresh_token : sig | ||
type t = | ||
{ client_id : string | ||
; client_secret : string option | ||
(** This field is present for web apps and absent for installed apps. *) | ||
; refresh_token : string | ||
} | ||
[@@deriving sexp] | ||
end | ||
|
||
module Userless_confidential : sig | ||
type t = | ||
{ client_id : string | ||
; client_secret : string | ||
} | ||
[@@deriving sexp] | ||
end | ||
|
||
module Userless_public : sig | ||
type t = | ||
{ client_id : string | ||
; device_id : string option | ||
} | ||
[@@deriving sexp] | ||
|
||
val device_id_or_default : t -> string | ||
end | ||
|
||
type t = | ||
| Password of Password.t | ||
| Refresh_token of Refresh_token.t | ||
| Userless_confidential of Userless_confidential.t | ||
| Userless_public of Userless_public.t | ||
[@@deriving sexp] | ||
|
||
val auth_header : t -> Cohttp.Header.t | ||
val basic_auth_string : t -> string | ||
val access_token_request_params : t -> (string * string list) list |
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
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