Add placeholder for avatar

This commit is contained in:
Simon Bihel 2022-02-08 10:36:38 +00:00
parent 4bce398253
commit 9d42c5a99b
No known key found for this signature in database
GPG Key ID: B7013150BEAA28FD

View File

@ -7,8 +7,8 @@ use iri_string::types::UriString;
use openidconnect::{ use openidconnect::{
core::{ core::{
CoreAuthErrorResponseType, CoreAuthPrompt, CoreClaimName, CoreClientAuthMethod, CoreAuthErrorResponseType, CoreAuthPrompt, CoreClaimName, CoreClientAuthMethod,
CoreClientMetadata, CoreClientRegistrationResponse, CoreErrorResponseType, CoreGrantType, CoreClientMetadata, CoreClientRegistrationResponse, CoreErrorResponseType, CoreGenderClaim,
CoreIdToken, CoreIdTokenClaims, CoreIdTokenFields, CoreJsonWebKeySet, CoreGrantType, CoreIdToken, CoreIdTokenClaims, CoreIdTokenFields, CoreJsonWebKeySet,
CoreJwsSigningAlgorithm, CoreProviderMetadata, CoreRegisterErrorResponseType, CoreJwsSigningAlgorithm, CoreProviderMetadata, CoreRegisterErrorResponseType,
CoreResponseType, CoreRsaPrivateSigningKey, CoreSubjectIdentifierType, CoreTokenResponse, CoreResponseType, CoreRsaPrivateSigningKey, CoreSubjectIdentifierType, CoreTokenResponse,
CoreTokenType, CoreUserInfoClaims, CoreUserInfoJsonWebToken, CoreTokenType, CoreUserInfoClaims, CoreUserInfoJsonWebToken,
@ -16,9 +16,10 @@ use openidconnect::{
registration::{EmptyAdditionalClientMetadata, EmptyAdditionalClientRegistrationResponse}, registration::{EmptyAdditionalClientMetadata, EmptyAdditionalClientRegistrationResponse},
url::Url, url::Url,
AccessToken, Audience, AuthUrl, ClientId, ClientSecret, EmptyAdditionalClaims, AccessToken, Audience, AuthUrl, ClientId, ClientSecret, EmptyAdditionalClaims,
EmptyAdditionalProviderMetadata, EmptyExtraTokenFields, EndUserUsername, IssuerUrl, EmptyAdditionalProviderMetadata, EmptyExtraTokenFields, EndUserPictureUrl, EndUserUsername,
JsonWebKeyId, JsonWebKeySetUrl, Nonce, PrivateSigningKey, RedirectUrl, RegistrationUrl, IssuerUrl, JsonWebKeyId, JsonWebKeySetUrl, LocalizedClaim, Nonce, PrivateSigningKey,
RequestUrl, ResponseTypes, Scope, StandardClaims, SubjectIdentifier, TokenUrl, UserInfoUrl, RedirectUrl, RegistrationUrl, RequestUrl, ResponseTypes, Scope, StandardClaims,
SubjectIdentifier, TokenUrl, UserInfoUrl,
}; };
use rsa::{pkcs1::ToRsaPrivateKey, RsaPrivateKey}; use rsa::{pkcs1::ToRsaPrivateKey, RsaPrivateKey};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -133,6 +134,7 @@ pub fn metadata(base_url: Url) -> Result<CoreProviderMetadata, CustomError> {
CoreClaimName::new("iat".to_string()), CoreClaimName::new("iat".to_string()),
CoreClaimName::new("iss".to_string()), CoreClaimName::new("iss".to_string()),
CoreClaimName::new("preferred_username".to_string()), CoreClaimName::new("preferred_username".to_string()),
CoreClaimName::new("picture".to_string()),
])) ]))
.set_registration_endpoint(Some(RegistrationUrl::from_url( .set_registration_endpoint(Some(RegistrationUrl::from_url(
base_url base_url
@ -171,6 +173,25 @@ async fn resolve_name(eth_provider: Option<Url>, address: H160) -> String {
} }
} }
async fn resolve_avatar(eth_provider: Option<Url>, address: H160) -> Option<Url> {
None
}
async fn resolve_claims(
eth_provider: Option<Url>,
address: H160,
) -> StandardClaims<CoreGenderClaim> {
StandardClaims::new(subject_id(&address))
.set_preferred_username(Some(EndUserUsername::new(
resolve_name(eth_provider.clone(), address).await,
)))
.set_picture(resolve_avatar(eth_provider, address).await.map(|a| {
let mut avatar_localized = LocalizedClaim::new();
avatar_localized.insert(None, EndUserPictureUrl::new(a.to_string()));
avatar_localized
}))
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct TokenForm { pub struct TokenForm {
pub code: String, pub code: String,
@ -244,9 +265,7 @@ pub async fn token(
vec![Audience::new(client_id.clone())], vec![Audience::new(client_id.clone())],
Utc::now() + Duration::seconds(60), Utc::now() + Duration::seconds(60),
Utc::now(), Utc::now(),
StandardClaims::new(subject_id(&code_entry.address)).set_preferred_username(Some( resolve_claims(eth_provider, code_entry.address).await,
EndUserUsername::new(resolve_name(eth_provider, code_entry.address).await),
)),
EmptyAdditionalClaims {}, EmptyAdditionalClaims {},
) )
.set_nonce(code_entry.nonce) .set_nonce(code_entry.nonce)
@ -591,9 +610,7 @@ pub async fn userinfo(
}; };
let response = CoreUserInfoClaims::new( let response = CoreUserInfoClaims::new(
StandardClaims::new(subject_id(&code_entry.address)).set_preferred_username(Some( resolve_claims(eth_provider, code_entry.address).await,
EndUserUsername::new(resolve_name(eth_provider, code_entry.address).await),
)),
EmptyAdditionalClaims::default(), EmptyAdditionalClaims::default(),
) )
.set_issuer(Some(IssuerUrl::from_url(base_url.clone()))) .set_issuer(Some(IssuerUrl::from_url(base_url.clone())))