//Example call: Authenticate("DirectoryV.3", "myDomain.com", "myUser); public static AuthenticatedUserInfo Authenticate(AuthenticatedUserInfo AuthUserInfo, ClientSecrets Secrets, string Domain = null, string User = null) { _currentAuthInfo = GetAuthTokenFlow(AuthUserInfo, Secrets); return(currentAuthInfo); }
/// <summary>Retrieve an authentication token from memory or from user authentication.</summary> /// <remarks>Also fills out the OAuth2Base currentAuthInfo and asyncUserCredential members.</remarks> public static AuthenticatedUserInfo GetAuthTokenFlow(AuthenticatedUserInfo AuthUserInfo, ClientSecrets Secrets, bool force = false) { //reset the auth info _currentAuthInfo = new AuthenticatedUserInfo() { scopes = AuthUserInfo.scopes, apiNameAndVersion = AuthUserInfo.apiNameAndVersion }; //First, if the domain or user are missing, see if we can fill it in using the defaults AuthUserInfo.domain = CheckDomain(AuthUserInfo.domain); if (AuthUserInfo.domain != null) { AuthUserInfo.userName = CheckUser(AuthUserInfo.domain, AuthUserInfo.userName); } //First, if we are able to load a key based on the domain and user, we do that and add it to the data store. // This will make sure that when we authenticate, the Google Flow has something to load. if (AuthUserInfo.userName != null && AuthUserInfo.domain != null && infoConsumer.TokenAndScopesExist( AuthUserInfo.domain, AuthUserInfo.userName, AuthUserInfo.apiNameAndVersion) && !force) { OAuth2TokenInfo preTokenInfo = infoConsumer.GetTokenInfo(AuthUserInfo.domain, AuthUserInfo.userName, AuthUserInfo.apiNameAndVersion); _currentAuthInfo.tokenResponse = preTokenInfo.token; _currentAuthInfo.tokenString = preTokenInfo.tokenString; _currentAuthInfo.scopes = preTokenInfo.scopes; //overwrite any coming in with what is saved } //Set the domain and user now, and we'll check them again later while we authorize. _currentAuthInfo.domain = AuthUserInfo.domain; _currentAuthInfo.userName = AuthUserInfo.userName; _IsAuthenticating = true; //Populate asyncUserCredential, but we don't quite save the token yet... AwaitUserCredential(AuthUserInfo.scopes, Secrets).Wait(); _IsAuthenticating = false; if (AuthTokenTempSwap != null) { //Now that we have asyncUserCredential filled out, we can actually save the token if we need to. memoryObjectDataStore.StoreAsync <TokenResponse>(string.Empty, AuthTokenTempSwap).Wait(); AuthTokenTempSwap = null; } //The scopes, domain, user and api have already been set above. the token is set while saving. return(currentAuthInfo); }
//public static BaseClientService.Initializer GetInitializer(Google.Apis.Http.IConfigurableHttpClientInitializer credentials) //{ // gInitializer initializer = new gInitializer() // { // HttpClientInitializer = credentials, // ApplicationName = _appName, // }; // return initializer; //} /// <summary> /// Returns an initializer used to create a new service. /// </summary> public static BaseClientService.Initializer GetInitializer(string AppName, AuthenticatedUserInfo authInfo, string serviceAccountUser = null) { //for non admin APIs, we need a service account //TODO: add in a default option to allow users to default to the DiscoveryInitializer anyways, if they want //if (authInfo.apiNameAndVersion.Contains("gmail") // || authInfo.apiNameAndVersion.Contains("drive")) if (string.IsNullOrWhiteSpace(serviceAccountUser)) { return(GetDiscoveryInitializer(AppName, authInfo)); } else { return(GetServiceInitializer(AppName, authInfo, serviceAccountUser)); } }
public static BaseClientService.Initializer GetServiceInitializer(string appName, AuthenticatedUserInfo authInfo, string serviceAccountUser) { var serviceAccount = infoConsumer.GetServiceAccount(authInfo.domain); var scopes = authInfo.scopes; ////TODO: this has to match exactly what the service account has been authorized for, and nothing more! //if (authInfo.apiNameAndVersion.Contains("gmail")) //{ // scopes = new string[] { "https://mail.google.com/" }; //} //else if (authInfo.apiNameAndVersion.Contains("drive")) //{ // scopes = new string[] { "https://www.googleapis.com/auth/drive" }; //} ServiceAccountCredential credential = null; if (serviceAccount.certType == OAuth2Domain.CertTypeEnum.json) { credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccount.email) { User = serviceAccountUser, Scopes = scopes }.FromPrivateKey(serviceAccount.privateKey)); } else { credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccount.email) { User = serviceAccountUser, Scopes = scopes }.FromCertificate(serviceAccount.certificate)); } var init = new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = appName, }; return(init); }
/// <summary> /// Returns an initializer used to create a new service for GData APIs. /// </summary> public static BaseClientService.Initializer GetGdataInitializer(string AppName, AuthenticatedUserInfo authInfo) { gXmlInitializer initializer = new gXmlInitializer() { HttpClientInitializer = asyncUserCredential, ApplicationName = AppName, //GZipEnabled = false }; return(initializer); }
public static BaseClientService.Initializer GetDiscoveryInitializer(string AppName, AuthenticatedUserInfo authInfo) { gJsonInitializer initializer = new gJsonInitializer() { HttpClientInitializer = asyncUserCredential, ApplicationName = AppName, }; return(initializer); }