Skip to content

Commit 48c4c68

Browse files
committed
datastore method for access token lookup by token
1 parent 4e46032 commit 48c4c68

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

nexus/db-lookup/src/lookup.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,11 @@ impl<'a> LookupPath<'a> {
219219
)
220220
}
221221

222+
// TODO: do the same conversion for this
223+
222224
/// Select a resource of type DeviceAccessToken, identified by its `token`
223-
pub fn device_access_token<'b, 'c>(
224-
self,
225-
token: &'b str,
226-
) -> DeviceAccessToken<'c>
227-
where
228-
'a: 'c,
229-
'b: 'c,
230-
{
231-
DeviceAccessToken::PrimaryKey(
232-
Root { lookup_root: self },
233-
token.to_string(),
234-
)
225+
pub fn device_access_token_id(self, id: Uuid) -> DeviceAccessToken<'a> {
226+
DeviceAccessToken::PrimaryKey(Root { lookup_root: self }, id)
235227
}
236228

237229
/// Select a resource of type RoleBuiltin, identified by its `name`
@@ -772,7 +764,7 @@ lookup_resource! {
772764
lookup_by_name = false,
773765
soft_deletes = false,
774766
primary_key_columns = [
775-
{ column_name = "token", rust_type = String },
767+
{ column_name = "id", rust_type = Uuid },
776768
]
777769
}
778770

nexus/db-queries/src/db/datastore/device_auth.rs

+18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use async_bb8_diesel::AsyncRunQueryDsl;
1313
use diesel::prelude::*;
1414
use nexus_db_errors::ErrorHandler;
1515
use nexus_db_errors::public_error_from_diesel;
16+
use nexus_db_schema::schema::device_access_token;
1617
use omicron_common::api::external::CreateResult;
1718
use omicron_common::api::external::Error;
1819
use omicron_common::api::external::LookupResult;
@@ -21,6 +22,23 @@ use omicron_common::api::external::ResourceType;
2122
use uuid::Uuid;
2223

2324
impl DataStore {
25+
pub async fn device_token_lookup_by_token(
26+
&self,
27+
opctx: &OpContext,
28+
token: String,
29+
) -> LookupResult<DeviceAccessToken> {
30+
// TODO: some special system authz because the presence of the token _is_ the authz
31+
device_access_token::table
32+
.filter(device_access_token::token.eq(token))
33+
.select(DeviceAccessToken::as_returning())
34+
.get_result_async(&*self.pool_connection_authorized(opctx).await?)
35+
.await
36+
.map_err(|_e| Error::ObjectNotFound {
37+
type_name: ResourceType::DeviceAccessToken,
38+
lookup_type: LookupType::ByOther("access token".to_string()),
39+
})
40+
}
41+
2442
/// Start a device authorization grant flow by recording the request
2543
/// and initial response parameters.
2644
pub async fn device_auth_request_create(

nexus/src/app/device_auth.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ impl super::Nexus {
166166
opctx: &OpContext,
167167
token: String,
168168
) -> Result<Actor, Reason> {
169-
let (.., db_access_token) = LookupPath::new(opctx, &self.db_datastore)
170-
.device_access_token(&token)
171-
.fetch()
169+
let db_access_token = self
170+
.db_datastore
171+
.device_token_lookup_by_token(opctx, token)
172172
.await
173173
.map_err(|e| match e {
174174
Error::ObjectNotFound { .. } => Reason::UnknownActor {

0 commit comments

Comments
 (0)