-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5757 from jwarwick/twitch
API Access to Game State
- Loading branch information
Showing
10 changed files
with
197 additions
and
19 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
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,43 @@ | ||
(ns web.api-keys | ||
(:require [web.mongodb :refer [object-id]] | ||
[web.utils :refer [response]] | ||
[web.ws :as ws] | ||
[monger.collection :as mc] | ||
[monger.result :refer [acknowledged?]]) | ||
(:import org.bson.types.ObjectId)) | ||
|
||
(defn api-keys-handler [{db :system/db | ||
{username :username} :user}] | ||
(if username | ||
(response 200 (mc/find-maps db "api-keys" {:username username})) | ||
(response 401 {:message "Unauthorized"}))) | ||
|
||
(defn api-keys-create-handler [{db :system/db | ||
{username :username} :user}] | ||
(if username | ||
(let [new-key (java.util.UUID/randomUUID) | ||
new-entry (mc/insert db "api-keys" | ||
{:username username | ||
:date (java.util.Date.) | ||
:api-key new-key})] | ||
(if (acknowledged? new-entry) | ||
(if (acknowledged? (mc/update db "users" {:username username} {"$set" {:has-api-keys true}})) | ||
(response 201 {:message "Created API Key"}) | ||
(response 500 {:message "Failed to update user info"})) | ||
(response 500 {:message "Failed to create API Key"}))) | ||
(response 401 {:message "Unauthorized"}))) | ||
|
||
(defn api-keys-delete-handler [{db :system/db | ||
{username :username} :user | ||
{id :id} :params}] | ||
(try | ||
(if (and username id) | ||
(if (acknowledged? (mc/remove db "api-keys" {:_id (object-id id) :username username})) | ||
(let [key-count (mc/count db "api-keys" {:username username})] | ||
(when-not (pos? key-count) | ||
(mc/update db "users" {:username username} {"$set" {:has-api-keys false}})) | ||
(response 200 {:message "Deleted"})) | ||
(response 403 {:message "Forbidden"})) | ||
(response 401 {:message "Unauthorized"})) | ||
(catch Exception ex | ||
(response 409 {:message "Unknown API Key"})))) |
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,49 @@ | ||
(ns web.game-api | ||
(:require [web.mongodb :refer [object-id]] | ||
[web.decks :as decks] | ||
[web.lobby :as lobby] | ||
[web.utils :refer [response]] | ||
[monger.collection :as mc])) | ||
|
||
(defn- make-link [host path] (str host path)) | ||
|
||
(defn- make-card-info [host card] | ||
{:title (:title (:card card)) | ||
:qty (:qty card) | ||
:image (make-link host (get-in card [:card :images :en :default :stock]))}) | ||
|
||
(defn- get-deck-id [username game] | ||
(if (:started game) | ||
(let [state @(:state game) | ||
side (if (= username (get-in state [:runner :user :username])) :runner :corp)] | ||
(get-in state [side :deck-id])) | ||
(:_id (:deck (first (filter #(= username (get-in % [:user :username])) (:players game))))))) | ||
|
||
(defn- get-deck [db username game] | ||
(if-let [deck-id (get-deck-id username game)] | ||
(decks/update-deck (mc/find-one-as-map db "decks" {:_id (object-id deck-id) :username username})) | ||
nil)) | ||
|
||
(defn deck-handler [{db :system/db | ||
scheme :scheme | ||
headers :headers}] | ||
(if-let [api-key (get headers "x-jnet-api")] | ||
(let [api-uuid (try | ||
(java.util.UUID/fromString api-key) | ||
(catch Exception e nil)) | ||
api-record (mc/find-one-as-map db "api-keys" {:api-key api-uuid} ["username"]) | ||
username (:username api-record)] | ||
(if username | ||
(let [game (lobby/game-for-username username) | ||
allow-access (:api-access game)] | ||
(if (and game allow-access) | ||
(if-let [deck (get-deck db username game)] | ||
(let [host (str (name scheme) "://" (get headers "host"))] | ||
(response 200 {:name (:name deck) | ||
:identity {:title (get-in deck [:identity :title]) | ||
:image (make-link host (get-in deck [:identity :images :en :default :stock]))} | ||
:cards (map #(make-card-info host %) (:cards deck))})) | ||
(response 204 {:message "No deck selected"})) | ||
(response 403 {:message "No game for key or API Access not enabled"}))) | ||
(response 404 {:message "Unknown X-JNet-API key"}))) | ||
(response 400 {:message "No X-JNet-API header specified"}))) |
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
Oops, something went wrong.