fedcm-mock.js (4668B)
1 import { RequestTokenStatus, LogoutRpsStatus, DisconnectStatus, FederatedAuthRequest, FederatedAuthRequestReceiver } from '/gen/third_party/blink/public/mojom/webid/federated_auth_request.mojom.m.js'; 2 3 function toMojoTokenStatus(status) { 4 return RequestTokenStatus["k" + status]; 5 } 6 7 // A mock service for responding to federated auth requests. 8 export class MockFederatedAuthRequest { 9 constructor() { 10 this.receiver_ = new FederatedAuthRequestReceiver(this); 11 this.interceptor_ = new MojoInterfaceInterceptor(FederatedAuthRequest.$interfaceName); 12 this.interceptor_.oninterfacerequest = e => { 13 this.receiver_.$.bindHandle(e.handle); 14 } 15 this.interceptor_.start(); 16 this.token_ = null; 17 this.selected_identity_provider_config_url_ = null; 18 this.status_ = RequestTokenStatus.kError; 19 this.logoutRpsStatus_ = LogoutRpsStatus.kError; 20 this.disconnectStatus_ = DisconnectStatus.kError; 21 this.returnPending_ = false; 22 this.pendingPromiseResolve_ = null; 23 } 24 25 // Causes the subsequent `navigator.credentials.get()` to resolve with the token. 26 returnToken(selected_identity_provider_config_url, token) { 27 this.status_ = RequestTokenStatus.kSuccess; 28 this.selected_identity_provider_config_url_ = selected_identity_provider_config_url; 29 this.token_ = token; 30 this.returnPending_ = false; 31 } 32 33 // Causes the subsequent `navigator.credentials.get()` to reject with the error. 34 returnError(error) { 35 if (error == "Success") 36 throw new Error("Success is not a valid error"); 37 this.status_ = toMojoTokenStatus(error); 38 this.selected_identity_provider_config_url_ = null; 39 this.token_ = null; 40 this.returnPending_ = false; 41 } 42 43 // Causes the subsequent `navigator.credentials.get()` to return a pending promise 44 // that can be cancelled using `cancelTokenRequest()`. 45 returnPendingPromise() { 46 this.returnPending_ = true; 47 } 48 49 logoutRpsReturn(status) { 50 let validated = LogoutRpsStatus[status]; 51 if (validated === undefined) 52 throw new Error("Invalid status: " + status); 53 this.logoutRpsStatus_ = validated; 54 } 55 56 // Causes the subsequent `FederatedCredential.disconnect` to reject with this 57 // status. 58 disconnectReturn(status) { 59 let validated = DisconnectStatus[status]; 60 if (validated === undefined) 61 throw new Error("Invalid status: " + status); 62 this.disconnectStatus_ = validated; 63 } 64 65 // Implements 66 // RequestToken(array<IdentityProviderGetParameters> idp_get_params) => 67 // (RequestTokenStatus status, 68 // url.mojom.Url? selected_identity_provider_config_url, 69 // string? token); 70 async requestToken(idp_get_params) { 71 if (this.returnPending_) { 72 this.pendingPromise_ = new Promise((resolve, reject) => { 73 this.pendingPromiseResolve_ = resolve; 74 }); 75 return this.pendingPromise_; 76 } 77 return Promise.resolve({ 78 status: this.status_, 79 selected_identity_provider_config_url: this.selected_identity_provider_config_url_, 80 token: this.token_ 81 }); 82 } 83 84 async cancelTokenRequest() { 85 this.pendingPromiseResolve_({ 86 status: toMojoTokenStatus("ErrorCanceled"), 87 selected_identity_provider_config_url: null, 88 token: null 89 }); 90 this.pendingPromiseResolve_ = null; 91 } 92 93 // Implements 94 // RequestUserInfo(IdentityProviderGetParameters idp_get_param) => 95 // (RequestUserInfoStatus status, array<IdentityUserInfo>? user_info); 96 async requestUserInfo(idp_get_param) { 97 return Promise.resolve({ 98 status: "", 99 user_info: "" 100 }); 101 } 102 103 async logoutRps(logout_endpoints) { 104 return Promise.resolve({ 105 status: this.logoutRpsStatus_ 106 }); 107 } 108 109 async disconnect(provider, client_id, account_id) { 110 return Promise.resolve({ 111 status: this.disconnectStatus_ 112 }); 113 } 114 115 async setIdpSigninStatus(origin, status) { 116 } 117 118 async registerIdP(configURL) { 119 } 120 121 async unregisterIdP(configURL) { 122 } 123 124 async resolveTokenRequest(token) { 125 } 126 127 async closeModalDialogView() { 128 } 129 130 async preventSilentAccess() { 131 } 132 133 async reset() { 134 this.token_ = null; 135 this.selected_identity_provider_config_url_ = null; 136 this.status_ = RequestTokenStatus.kError; 137 this.logoutRpsStatus_ = LogoutRpsStatus.kError; 138 this.disconnectStatus_ = DisconnectStatus.kError; 139 this.receiver_.$.close(); 140 this.interceptor_.stop(); 141 142 // Clean up and reset mock stubs asynchronously, so that the blink side 143 // closes its proxies and notifies JS sensor objects before new test is 144 // started. 145 await new Promise(resolve => { step_timeout(resolve, 0); }); 146 } 147 }