Skip to content

Commit

Permalink
Merge pull request #11 from Authress/enable-auth-endpoint-without-red…
Browse files Browse the repository at this point in the history
…irect

Allow fetching auth token using api without redirect url.
  • Loading branch information
wparad authored Jan 20, 2024
2 parents ead68d3 + 33ac2a3 commit 980d527
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
33 changes: 23 additions & 10 deletions examples/server/authentication_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ pub struct OpenIdConfiguration {
pub authorization_endpoint: String
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
pub struct StartAuthenticationResponse {
#[serde(rename = "authenticationUrl")]
pub authentication_url: String,
#[serde(rename = "authenticationUrl", skip_serializing_if="Option::is_none")]
pub authentication_url: Option<String>,

#[serde(rename = "authenticationRequestId")]
pub authentication_request_id: String
#[serde(rename = "authenticationRequestId", skip_serializing_if="Option::is_none")]
pub authentication_request_id: Option<String>,

#[serde(rename = "accessToken", skip_serializing_if="Option::is_none")]
pub access_token: Option<String>
}

#[derive(Debug, Default, Clone, Copy)]
Expand All @@ -35,13 +38,23 @@ impl AuthenticationController {
let access_token = signature_key.create_token(host);
let id_token = signature_key.create_id_token(host);

let url = Url::parse_with_params(&authentication_request.redirect_url,
&[("access_token", &access_token), ("id_token", &id_token), ("nonce", &request_id.to_string())]
).unwrap();
if let Some(redirect_url) = authentication_request.redirect_url {
let parsed_url = Url::parse_with_params(&redirect_url,
&[("access_token", &access_token), ("id_token", &id_token), ("nonce", &request_id.to_string())]
);
if let Ok(url) = parsed_url {
return StartAuthenticationResponse {
authentication_request_id: Some(request_id.to_string()),
authentication_url: Some(url.to_string()),
access_token: Some(access_token)
}
}
}

return StartAuthenticationResponse {
authentication_request_id: request_id.to_string(),
authentication_url: url.to_string()
authentication_request_id: None,
authentication_url: None,
access_token: Some(access_token)
}
}

Expand Down
17 changes: 1 addition & 16 deletions src/authentication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub enum AuthenticationResponse {
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct AuthenticationRequest {
#[serde(rename = "redirectUrl")]
pub redirect_url: String,
pub redirect_url: Option<String>,
// /// The secret associated with the client that authorizes the generation of token it's behalf. (Either the `client_secret` or the `code_verifier` is required)
// #[serde(rename = "client_secret", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
// pub client_secret: Option<Option<String>>,
Expand All @@ -81,18 +81,3 @@ pub struct AuthenticationRequest {
// #[serde(rename = "type", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
// pub r#type: Option<Option<Type>>,
}

impl AuthenticationRequest {
pub fn new(redirect_url: String) -> AuthenticationRequest {
AuthenticationRequest {
redirect_url,
// client_id,
// client_secret: None,
// code_verifier: None,
// grant_type: None,
// username: None,
// password: None,
// r#type: None,
}
}
}

0 comments on commit 980d527

Please sign in to comment.