Skip to content

Commit 0f88ed1

Browse files
committed
Add public methods for instantiating TokenResponse and ErrorResponse.
1 parent 0d306c1 commit 0f88ed1

File tree

2 files changed

+141
-4
lines changed

2 files changed

+141
-4
lines changed

src/lib.rs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,11 +1304,67 @@ where
13041304
TT: TokenType,
13051305
{
13061306
///
1307-
/// Extra fields defined by client application.
1307+
/// Instantiate a new OAuth2 token response.
1308+
///
1309+
pub fn new(access_token: AccessToken, token_type: TT, extra_fields: EF) -> Self {
1310+
Self {
1311+
access_token,
1312+
token_type,
1313+
expires_in: None,
1314+
refresh_token: None,
1315+
scopes: None,
1316+
extra_fields,
1317+
}
1318+
}
1319+
1320+
///
1321+
/// Set the `access_token` field.
1322+
///
1323+
pub fn set_access_token(&mut self, access_token: AccessToken) {
1324+
self.access_token = access_token;
1325+
}
1326+
1327+
///
1328+
/// Set the `token_type` field.
1329+
///
1330+
pub fn set_token_type(&mut self, token_type: TT) {
1331+
self.token_type = token_type;
1332+
}
1333+
1334+
///
1335+
/// Set the `expires_in` field.
1336+
///
1337+
pub fn set_expires_in(&mut self, expires_in: Option<u64>) {
1338+
self.expires_in = expires_in;
1339+
}
1340+
1341+
///
1342+
/// Set the `refresh_token` field.
1343+
///
1344+
pub fn set_refresh_token(&mut self, refresh_token: Option<RefreshToken>) {
1345+
self.refresh_token = refresh_token;
1346+
}
1347+
1348+
///
1349+
/// Set the `scopes` field.
1350+
///
1351+
pub fn set_scopes(&mut self, scopes: Option<Vec<Scope>>) {
1352+
self.scopes = scopes;
1353+
}
1354+
1355+
///
1356+
/// Extra fields defined by the client application.
13081357
///
13091358
pub fn extra_fields(&self) -> &EF {
13101359
&self.extra_fields
13111360
}
1361+
1362+
///
1363+
/// Set the extra fields defined by the client application.
1364+
///
1365+
pub fn set_extra_fields(&mut self, extra_fields: EF) {
1366+
self.extra_fields = extra_fields;
1367+
}
13121368
}
13131369

13141370
impl<EF, TT> TokenResponse<TT> for StandardTokenResponse<EF, TT>
@@ -1392,6 +1448,30 @@ pub struct ErrorResponse<T: ErrorResponseType> {
13921448
}
13931449

13941450
impl<T: ErrorResponseType> ErrorResponse<T> {
1451+
///
1452+
/// Instantiate a new `ErrorResponse`.
1453+
///
1454+
/// # Arguments
1455+
///
1456+
/// * `error` - REQUIRED. A single ASCII error code deserialized to the generic parameter.
1457+
/// `ErrorResponseType`.
1458+
/// * `error_description` - OPTIONAL. Human-readable ASCII text providing additional
1459+
/// information, used to assist the client developer in understanding the error that
1460+
/// occurred. Values for this parameter MUST NOT include characters outside the set
1461+
/// `%x20-21 / %x23-5B / %x5D-7E`.
1462+
/// * `error_uri` - OPTIONAL. A URI identifying a human-readable web page with information
1463+
/// about the error used to provide the client developer with additional information about
1464+
/// the error. Values for the "error_uri" parameter MUST conform to the URI-reference
1465+
/// syntax and thus MUST NOT include characters outside the set `%x21 / %x23-5B / %x5D-7E`.
1466+
///
1467+
pub fn new(error: T, error_description: Option<String>, error_uri: Option<String>) -> Self {
1468+
Self {
1469+
error,
1470+
error_description,
1471+
error_uri,
1472+
}
1473+
}
1474+
13951475
///
13961476
/// REQUIRED. A single ASCII error code deserialized to the generic parameter
13971477
/// `ErrorResponseType`.
@@ -1401,14 +1481,17 @@ impl<T: ErrorResponseType> ErrorResponse<T> {
14011481
}
14021482
///
14031483
/// OPTIONAL. Human-readable ASCII text providing additional information, used to assist
1404-
/// the client developer in understanding the error that occurred.
1484+
/// the client developer in understanding the error that occurred. Values for this
1485+
/// parameter MUST NOT include characters outside the set `%x20-21 / %x23-5B / %x5D-7E`.
14051486
///
14061487
pub fn error_description(&self) -> Option<&String> {
14071488
self.error_description.as_ref()
14081489
}
14091490
///
14101491
/// OPTIONAL. A URI identifying a human-readable web page with information about the error,
14111492
/// used to provide the client developer with additional information about the error.
1493+
/// Values for the "error_uri" parameter MUST conform to the URI-reference syntax and
1494+
/// thus MUST NOT include characters outside the set `%x21 / %x23-5B / %x5D-7E`.
14121495
///
14131496
pub fn error_uri(&self) -> Option<&String> {
14141497
self.error_uri.as_ref()

tests/lib.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,9 @@ mod colorful_extension {
914914
pub struct ColorfulFields {
915915
#[serde(rename = "shape")]
916916
#[serde(skip_serializing_if = "Option::is_none")]
917-
shape: Option<String>,
917+
pub shape: Option<String>,
918918
#[serde(rename = "height")]
919-
height: u32,
919+
pub height: u32,
920920
}
921921
impl ColorfulFields {
922922
pub fn shape(&self) -> Option<&String> {
@@ -1142,6 +1142,60 @@ fn test_extension_with_simple_json_error() {
11421142
);
11431143
}
11441144

1145+
#[test]
1146+
fn test_extension_serializer() {
1147+
use colorful_extension::{ColorfulFields, ColorfulTokenResponse, ColorfulTokenType};
1148+
let mut token_response = ColorfulTokenResponse::new(
1149+
AccessToken::new("mysecret".to_string()),
1150+
ColorfulTokenType::Red,
1151+
ColorfulFields {
1152+
shape: Some("circle".to_string()),
1153+
height: 10,
1154+
},
1155+
);
1156+
token_response.set_expires_in(Some(3600));
1157+
token_response.set_refresh_token(Some(RefreshToken::new("myothersecret".to_string())));
1158+
let serialized = serde_json::to_string(&token_response).unwrap();
1159+
assert_eq!(
1160+
"{\
1161+
\"access_token\":\"mysecret\",\
1162+
\"token_type\":\"red\",\
1163+
\"expires_in\":3600,\
1164+
\"refresh_token\":\"myothersecret\",\
1165+
\"shape\":\"circle\",\
1166+
\"height\":10\
1167+
}",
1168+
serialized,
1169+
);
1170+
}
1171+
1172+
#[test]
1173+
fn test_error_response_serializer() {
1174+
assert_eq!(
1175+
"{\"error\":\"unauthorized_client\"}",
1176+
serde_json::to_string(&BasicErrorResponse::new(
1177+
BasicErrorResponseType::UnauthorizedClient,
1178+
None,
1179+
None,
1180+
))
1181+
.unwrap(),
1182+
);
1183+
1184+
assert_eq!(
1185+
"{\
1186+
\"error\":\"invalid_client\",\
1187+
\"error_description\":\"Invalid client_id\",\
1188+
\"error_uri\":\"https://example.com/errors/invalid_client\"\
1189+
}",
1190+
serde_json::to_string(&BasicErrorResponse::new(
1191+
BasicErrorResponseType::InvalidClient,
1192+
Some("Invalid client_id".to_string()),
1193+
Some("https://example.com/errors/invalid_client".to_string()),
1194+
))
1195+
.unwrap(),
1196+
);
1197+
}
1198+
11451199
#[test]
11461200
fn test_secret_redaction() {
11471201
let secret = ClientSecret::new("top_secret".to_string());

0 commit comments

Comments
 (0)