Skip to main content

Mountain/RPC/CocoonService/Auth/
RegisterAuthenticationProvider.rs

1
2//! Register an authentication provider in `ApplicationState`. Cocoon-side
3//! providers (GitHub, Microsoft, etc.) call this on activation; later
4//! `GetAuthenticationSession` calls look up the registered handle.
5
6use serde_json::json;
7use tonic::{Response, Status};
8use CommonLibrary::LanguageFeature::DTO::ProviderType::ProviderType;
9
10use crate::{
11	ApplicationState::DTO::ProviderRegistrationDTO::ProviderRegistrationDTO,
12	RPC::CocoonService::CocoonServiceImpl,
13	Vine::Generated::{Empty, RegisterAuthenticationProviderRequest},
14	dev_log,
15};
16
17pub async fn Fn(
18	Service:&CocoonServiceImpl,
19
20	Request:RegisterAuthenticationProviderRequest,
21) -> Result<Response<Empty>, Status> {
22	dev_log!(
23		"cocoon",
24		"[CocoonService] Registering Authentication Provider: id={}",
25		Request.id
26	);
27
28	let Handle = Request
29		.id
30		.as_bytes()
31		.iter()
32		.fold(0u32, |Acc, B| Acc.wrapping_mul(31).wrapping_add(*B as u32));
33
34	let DTO = ProviderRegistrationDTO {
35		Handle,
36
37		ProviderType:ProviderType::Authentication,
38
39		Selector:json!([{ "provider": Request.id }]),
40
41		SideCarIdentifier:"cocoon-main".to_string(),
42
43		ExtensionIdentifier:json!(Request.extension_id),
44
45		Options:Some(json!({
46			"label": Request.label,
47			"supportsMultipleAccounts": Request.supports_multiple_accounts,
48		})),
49	};
50
51	Service
52		.environment
53		.ApplicationState
54		.Extension
55		.ProviderRegistration
56		.RegisterProvider(Handle, DTO);
57
58	Ok(Response::new(Empty {}))
59}