Skip to content

Commit

Permalink
[PLATFORM-1248]: Look into adding a "sub" field to the access token (#72
Browse files Browse the repository at this point in the history
)

* Add 'sub' and other RFC claims fields

* Format
  • Loading branch information
cottinisimone authored Nov 28, 2023
1 parent ffe847e commit afff4be
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct Config {
#[serde(default = "defaults::issuer")]
issuer: String,

#[serde(default = "defaults::subject")]
subject: String,

#[serde(default)]
user_info: UserInfo,

Expand Down Expand Up @@ -57,6 +60,7 @@ impl Config {
log_error(error);
Self {
issuer: defaults::issuer(),
subject: defaults::subject(),
user_info: Default::default(),
audience: vec![],
user: vec![],
Expand Down
1 change: 1 addition & 0 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ fn new_token_response(
audience.to_string(),
permissions,
app_data.config().issuer().to_string(),
app_data.config().subject().to_string(),
grant_type,
custom_claims,
);
Expand Down
30 changes: 23 additions & 7 deletions src/model/claims.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
use std::fmt::{Display, Formatter};

use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
use uuid::Uuid;

use crate::config::{CustomField, CustomFieldValue};

#[derive(Debug, Deserialize)]
pub struct Claims {
iss: String,
sub: String,
aud: String,
iat: Option<i64>,
exp: Option<i64>,
nbf: Option<i64>,
iat: Option<i64>,
jti: String,
scope: String,
iss: String,
gty: GrantType,
permissions: Vec<String>,
// skip deserializing since deserialization from a jwt wouldn't match this struct
Expand All @@ -23,15 +28,19 @@ impl Claims {
aud: String,
permissions: Vec<String>,
iss: String,
sub: String,
gty: GrantType,
custom_claims: Vec<CustomField>,
) -> Self {
Self {
iss,
sub,
aud,
iat: Some(chrono::Utc::now().timestamp()),
exp: Some(chrono::Utc::now().timestamp() + 60000),
nbf: Some(chrono::Utc::now().timestamp()),
iat: Some(chrono::Utc::now().timestamp()),
jti: Uuid::new_v4().to_string(),
scope: permissions.join(" "),
iss,
gty,
permissions,
custom_claims,
Expand All @@ -50,6 +59,10 @@ impl Claims {
&self.iss
}

pub fn subject(&self) -> &str {
&self.sub
}

pub fn grant_type(&self) -> &GrantType {
&self.gty
}
Expand All @@ -67,11 +80,14 @@ impl Serialize for Claims {
{
let mut map = serializer.serialize_map(None)?;

map.serialize_entry("iss", &self.iss)?;
map.serialize_entry("sub", &self.sub)?;
map.serialize_entry("aud", &self.aud)?;
map.serialize_entry("iat", &self.iat)?;
map.serialize_entry("exp", &self.exp)?;
map.serialize_entry("nbf", &self.nbf)?;
map.serialize_entry("iat", &self.iat)?;
map.serialize_entry("jti", &self.jti)?;
map.serialize_entry("scope", &self.scope)?;
map.serialize_entry("iss", &self.iss)?;
map.serialize_entry("gty", &self.gty)?;
map.serialize_entry("permissions", &self.permissions)?;

Expand Down
7 changes: 6 additions & 1 deletion src/model/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use chrono::{DateTime, Utc};

const ISSUER: &str = "https://prima.localauth0.com/";
const USER_INFO_SUBJECT: &str = "google-apps|[email protected]";

const SUBJECT: &str = "google-apps|[email protected]";
const USER_INFO_SUBJECT: &str = SUBJECT;
const USER_INFO_NAME: &str = "Local";
const USER_INFO_NICKNAME: &str = "locie.auth0";
const USER_INFO_GIVEN_NAME: &str = "Locie";
Expand All @@ -21,6 +23,9 @@ pub fn issuer() -> String {
ISSUER.to_string()
}

pub fn subject() -> String {
SUBJECT.to_string()
}
pub fn user_info_subject() -> String {
USER_INFO_SUBJECT.to_string()
}
Expand Down
6 changes: 6 additions & 0 deletions src/model/jwks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ mod tests {
let audience: &str = "audience";
let permission: &str = "permission";
let issuer: &str = "issuer";
let subject: &str = "subject";
let gty: GrantType = GrantType::ClientCredentials;

let jwks: Jwks = jwk_store.get().unwrap();
Expand All @@ -185,6 +186,7 @@ mod tests {
audience.to_string(),
vec![permission.to_string()],
issuer.to_string(),
subject.to_string(),
gty.clone(),
vec![],
);
Expand All @@ -206,6 +208,7 @@ mod tests {
let audience: &str = "audience";
let permission: &str = "permission";
let issuer: &str = "issuer";
let subject: &str = "subject";
let gty: GrantType = GrantType::ClientCredentials;

let jwks: Jwks = jwk_store.get().unwrap();
Expand All @@ -219,6 +222,7 @@ mod tests {
audience.to_string(),
vec![permission.to_string()],
issuer.to_string(),
subject.to_string(),
gty,
custom_claims,
);
Expand All @@ -237,6 +241,7 @@ mod tests {
let audience: &str = "audience";
let permission: &str = "permission";
let issuer: &str = "issuer";
let subject: &str = "subject";
let gty: GrantType = GrantType::ClientCredentials;

let jwks: Jwks = jwk_store.get().unwrap();
Expand All @@ -252,6 +257,7 @@ mod tests {
audience.to_string(),
vec![permission.to_string()],
issuer.to_string(),
subject.to_string(),
gty,
custom_claims,
);
Expand Down

0 comments on commit afff4be

Please sign in to comment.