Skip to main content

Mountain/RPC/CocoonService/Auth/
RegisterAuthenticationProvider.rs

1#![allow(non_snake_case)]
2
3//! Register an authentication provider in `ApplicationState`. Cocoon-side
4//! providers (GitHub, Microsoft, etc.) call this on activation; later
5//! `GetAuthenticationSession` calls look up the registered handle.
6
7use serde_json::json;
8use tonic::{Response, Status};
9use CommonLibrary::LanguageFeature::DTO::ProviderType::ProviderType;
10
11use crate::{
12	ApplicationState::DTO::ProviderRegistrationDTO::ProviderRegistrationDTO,
13	RPC::CocoonService::CocoonServiceImpl,
14	Vine::Generated::{Empty, RegisterAuthenticationProviderRequest},
15	dev_log,
16};
17
18pub async fn Fn(
19	Service:&CocoonServiceImpl,
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	let DTO = ProviderRegistrationDTO {
34		Handle,
35		ProviderType:ProviderType::Authentication,
36		Selector:json!([{ "provider": Request.id }]),
37		SideCarIdentifier:"cocoon-main".to_string(),
38		ExtensionIdentifier:json!(Request.extension_id),
39		Options:Some(json!({
40			"label": Request.label,
41			"supportsMultipleAccounts": Request.supports_multiple_accounts,
42		})),
43	};
44	Service
45		.environment
46		.ApplicationState
47		.Extension
48		.ProviderRegistration
49		.RegisterProvider(Handle, DTO);
50
51	Ok(Response::new(Empty {}))
52}