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
21	Request:RegisterAuthenticationProviderRequest,
22) -> Result<Response<Empty>, Status> {
23	dev_log!(
24		"cocoon",
25		"[CocoonService] Registering Authentication Provider: id={}",
26		Request.id
27	);
28
29	let Handle = Request
30		.id
31		.as_bytes()
32		.iter()
33		.fold(0u32, |Acc, B| Acc.wrapping_mul(31).wrapping_add(*B as u32));
34
35	let DTO = ProviderRegistrationDTO {
36		Handle,
37
38		ProviderType:ProviderType::Authentication,
39
40		Selector:json!([{ "provider": Request.id }]),
41
42		SideCarIdentifier:"cocoon-main".to_string(),
43
44		ExtensionIdentifier:json!(Request.extension_id),
45
46		Options:Some(json!({
47			"label": Request.label,
48			"supportsMultipleAccounts": Request.supports_multiple_accounts,
49		})),
50	};
51
52	Service
53		.environment
54		.ApplicationState
55		.Extension
56		.ProviderRegistration
57		.RegisterProvider(Handle, DTO);
58
59	Ok(Response::new(Empty {}))
60}