Skip to content

Commit 9d92e90

Browse files
committed
add endpoint to docs and 400 error result
1 parent 6937d9f commit 9d92e90

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

openapi.yml

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,14 @@ paths:
8383
content:
8484
application/json:
8585
schema:
86-
type: string
87-
description: The token used for authentication
88-
example: "a1b2c3d4-1234-5678-9abc-1234567890ab"
86+
type: object
87+
properties:
88+
error:
89+
type: "null"
90+
payload:
91+
type: string
92+
description: The token used for authentication
93+
example: "a1b2c3d4-1234-5678-9abc-1234567890ab"
8994
"400":
9095
description: Bad request - for different reasons, like IP mismatch, interval not being respected, etc.
9196
content:
@@ -100,6 +105,53 @@ paths:
100105
"500":
101106
$ref: "#/components/responses/InternalServerError"
102107

108+
/v1/login/github/token:
109+
post:
110+
tags:
111+
- user
112+
summary: Login with GitHub access token
113+
description: Log in immediately with a GitHub access token. Returns a token if the login was successful.
114+
requestBody:
115+
content:
116+
application/json:
117+
schema:
118+
type: object
119+
properties:
120+
token:
121+
type: string
122+
description: The access token from GitHub
123+
example: "ghp_1234567890abcdefgh"
124+
responses:
125+
"200":
126+
description: OK
127+
content:
128+
application/json:
129+
schema:
130+
type: object
131+
properties:
132+
error:
133+
type: "null"
134+
payload:
135+
type: object
136+
properties:
137+
type: string
138+
description: The Geode token used for authentication
139+
example: "a1b2c3d4-1234-5678-9abc-1234567890ab"
140+
"400":
141+
description: Bad request - invalid access token.
142+
content:
143+
application/json:
144+
schema:
145+
type: object
146+
properties:
147+
error:
148+
type: string
149+
payload:
150+
type: "null"
151+
"500":
152+
$ref: "#/components/responses/InternalServerError"
153+
154+
103155
/v1/mods:
104156
get:
105157
tags:

src/auth/github.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl GithubClient {
164164
}
165165
}
166166

167-
pub async fn get_user(&self, token: String) -> Result<serde_json::Value, ApiError> {
167+
pub async fn get_user(&self, token: &str) -> Result<serde_json::Value, ApiError> {
168168
let client = Client::new();
169169
let resp = match client
170170
.get("https://api.github.com/user")

src/endpoints/auth/github.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ struct TokenLoginParams {
2323
}
2424

2525
async fn developer_from_token(
26-
client: &github::GithubClient,
2726
pool: &mut PgConnection,
28-
token: String
27+
user: serde_json::Value
2928
) -> Result<Uuid, Option<ApiError>> {
30-
let user = client.get_user(token).await?;
31-
3229
let id = user.get("id").ok_or(None)?.as_i64().unwrap();
3330
let username = user.get("login").ok_or(None)?;
3431

@@ -169,9 +166,10 @@ pub async fn poll_github_login(
169166
.await
170167
.or(Err(ApiError::TransactionError))?;
171168

169+
let user = client.get_user(&token).await.map_err(|_| ApiError::InternalError)?;
172170
let mut transaction = pool.begin().await.or(Err(ApiError::TransactionError))?;
173171

174-
let token = match developer_from_token(&client, &mut transaction, token).await {
172+
let token = match developer_from_token(&mut transaction, user).await {
175173
Ok(t) => t,
176174
Err(e) => {
177175
if let Some(e) = e {
@@ -196,15 +194,18 @@ pub async fn github_token_login(
196194
json: web::Json<TokenLoginParams>,
197195
data: web::Data<AppData>,
198196
) -> Result<impl Responder, ApiError> {
199-
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
200-
let mut transaction = pool.begin().await.or(Err(ApiError::TransactionError))?;
201-
202197
let client = github::GithubClient::new(
203198
data.github_client_id.to_string(),
204199
data.github_client_secret.to_string(),
205200
);
206201

207-
let token = match developer_from_token(&client, &mut transaction, json.token.to_string()).await {
202+
let user = client.get_user(&json.token).await
203+
.map_err(|_| ApiError::BadRequest(format!("Invalid access token: {}", json.token)))?;
204+
205+
let mut pool = data.db.acquire().await.or(Err(ApiError::DbAcquireError))?;
206+
let mut transaction = pool.begin().await.or(Err(ApiError::TransactionError))?;
207+
208+
let token = match developer_from_token(&mut transaction, user).await {
208209
Ok(t) => t,
209210

210211
Err(e) => {

0 commit comments

Comments
 (0)