Example #1
0
        /// <summary>
        /// Refresh the credentials (internal & external)
        /// </summary>
        /// <returns></returns>
        public async Task RefreshAsync()
        {
            if (ExpiresAt > DateTime.Now)
            {
                return;
            }

            ThreeLeggedApi oauth = new ThreeLeggedApi();

            dynamic credentialInternal = await oauth.RefreshtokenAsync(
                Config.GetAppSetting("FORGE_CLIENT_ID"), Config.GetAppSetting("FORGE_CLIENT_SECRET"),
                "refresh_token", RefreshToken, new Scope[] { Scope.DataRead, Scope.DataCreate, Scope.DataWrite, Scope.ViewablesRead });

            dynamic credentialPublic = await oauth.RefreshtokenAsync(
                Config.GetAppSetting("FORGE_CLIENT_ID"), Config.GetAppSetting("FORGE_CLIENT_SECRET"),
                "refresh_token", credentialInternal.refresh_token, new Scope[] { Scope.ViewablesRead });

            TokenInternal = credentialInternal.access_token;
            TokenPublic   = credentialPublic.access_token;
            RefreshToken  = credentialPublic.refresh_token;
            ExpiresAt     = DateTime.Now.AddSeconds(credentialInternal.expires_in);

            // update the record on our database for the tokens and refresh token
            await OAuthDB.Register(await GetUserId(this), JsonConvert.SerializeObject(this));
        }
        /// <summary>
        /// Perform the OAuth authorization via code
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static async Task <Credentials> CreateFromCodeAsync(string code, IResponseCookies cookies)
        {
            ThreeLeggedApi oauth = new ThreeLeggedApi();

            dynamic credentialInternal = await oauth.GettokenAsync(
                GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting("FORGE_CLIENT_SECRET"),
                oAuthConstants.AUTHORIZATION_CODE, code, GetAppSetting("FORGE_CALLBACK_URL")
                );

            dynamic credentialPublic = await oauth.RefreshtokenAsync(
                GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting("FORGE_CLIENT_SECRET"),
                "refresh_token", credentialInternal.refresh_token, new Scope[] { Scope.ViewablesRead }
                );

            Credentials credentials = new Credentials();

            credentials.TokenInternal = credentialInternal.access_token;
            credentials.TokenPublic   = credentialPublic.access_token;
            credentials.RefreshToken  = credentialPublic.refresh_token;
            credentials.ExpiresAt     = DateTime.Now.AddSeconds(credentialInternal.expires_in);

            cookies.Append(FORGE_COOKIE, JsonConvert.SerializeObject(credentials));

            return(credentials);
        }
Example #3
0
        public static async Task <string> GetAccessToken(string sessionId, string localId)
        {
            var filterBuilder = Builders <BsonDocument> .Filter;
            var filter        = filterBuilder.Eq("_id", new ObjectId(sessionId)) & filterBuilder.Eq("local_id", localId);
            var users         = Database.GetCollection <BsonDocument>("users");
            var docs          = await users.Find(filter).ToListAsync();

            var doc         = docs.First();
            var accessToken = (string)doc["access_token"];

            DateTime expiresAt = (DateTime)doc["expires_at"];

            if (expiresAt < DateTime.UtcNow)
            {
                // refresh the access_token maintaining the session id (document ID on the database)
                ThreeLeggedApi    oauth  = new ThreeLeggedApi();
                DynamicDictionary bearer = await oauth.RefreshtokenAsync(ConfigVariables.FORGE_CLIENT_ID, ConfigVariables.FORGE_CLIENT_SECRET, "refresh_token", (string)doc["refresh_token"]);

                var update = Builders <BsonDocument> .Update
                             .Set("access_token", (string)bearer.Dictionary["access_token"])
                             .Set("refresh_token", (string)bearer.Dictionary["refresh_token"])
                             .Set("expires_at", DateTime.UtcNow.AddSeconds((long)bearer.Dictionary["expires_in"]));

                var result = await users.UpdateOneAsync(filter, update);

                accessToken = (string)bearer.Dictionary["access_token"];
            }

            return(accessToken);
        }
Example #4
0
        /// <summary>
        /// Refresh the credentials (internal & external)
        /// </summary>
        /// <returns></returns>
        private async Task RefreshAsync()
        {
            ThreeLeggedApi oauth = new ThreeLeggedApi();

            dynamic credentialInternal = await oauth.RefreshtokenAsync(
                GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting("FORGE_CLIENT_SECRET"),
                "refresh_token", RefreshToken, new Scope[] { Scope.DataRead, Scope.DataCreate, Scope.DataWrite, Scope.ViewablesRead });

            dynamic credentialPublic = await oauth.RefreshtokenAsync(
                GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting("FORGE_CLIENT_SECRET"),
                "refresh_token", credentialInternal.refresh_token, new Scope[] { Scope.ViewablesRead });

            TokenInternal = credentialInternal.access_token;
            TokenPublic   = credentialPublic.access_token;
            RefreshToken  = credentialPublic.refresh_token;
            ExpiresAt     = DateTime.Now.AddSeconds(credentialInternal.expires_in);
        }
        private async Task <Credentials> RefreshAsync()
        {
            ThreeLeggedApi oauth = new ThreeLeggedApi();

            dynamic credentialInteral = await oauth.RefreshtokenAsync(
                Startup.Configuration["ForgeAPIID:FORGE_CLIENT_ID"], Startup.Configuration["ForgeAPIID:FORGE_CLIENT_SECRET"],
                "refresh_token", RefreshToken, new Scope[] { Scope.ViewablesRead });

            dynamic credentialPublic = await oauth.RefreshtokenAsync(
                Startup.Configuration["ForgeAPIID:FORGE_CLIENT_ID"], Startup.Configuration["ForgeAPIID:FORGE_CLIENT_SECRET"],
                "refresh_token", credentialInteral.refresh_token, new Scope[] { Scope.ViewablesRead });

            Credentials credentials = new Credentials
            {
                TokenInternal = credentialInteral.access_token,
                TokenPublic   = credentialPublic.access_token,
                RefreshToken  = credentialPublic.refresh_token,
                ExpiresAt     = DateTime.Now.AddSeconds(credentialInteral.expires_in)
            };

            return(credentials);
        }
        /// <summary>
        /// Refresh the credentials (internal & external)
        /// </summary>
        /// <returns></returns>
        private async Task RefreshAsync()
        {
            ThreeLeggedApi oauth     = new ThreeLeggedApi();
            TwoLeggedApi   da4rOauth = new TwoLeggedApi(); // added for DA4R

            dynamic credentialInternal = await oauth.RefreshtokenAsync(
                GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting("FORGE_CLIENT_SECRET"),
                "refresh_token", RefreshToken, new Scope[] { Scope.DataRead, Scope.DataCreate, Scope.DataWrite, Scope.ViewablesRead });

            dynamic credentialPublic = await oauth.RefreshtokenAsync(
                GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting("FORGE_CLIENT_SECRET"),
                "refresh_token", credentialInternal.refresh_token, new Scope[] { Scope.ViewablesRead });

            dynamic credentialDa4rInternal = await da4rOauth.AuthenticateAsync(
                GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting("FORGE_CLIENT_SECRET"),
                "client_credentials", new Scope[] { Scope.BucketCreate, Scope.BucketRead, Scope.BucketUpdate, Scope.DataRead, Scope.DataWrite, Scope.DataCreate, Scope.CodeAll });

            TokenInternal     = credentialInternal.access_token;
            TokenPublic       = credentialPublic.access_token;
            DA4RTokenInternal = credentialDa4rInternal.access_token; //added for DA4R
            RefreshToken      = credentialPublic.refresh_token;
            ExpiresAt         = DateTime.Now.AddSeconds(credentialInternal.expires_in);
        }
Example #7
0
        public static async Task <Credencial> CreateFromCodeAsync(string code, IResponseCookies cookies)
        {
            ThreeLeggedApi oauth = new ThreeLeggedApi();

            dynamic credentialInternal = await oauth.GettokenAsync(
                GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting("FORGE_CLIENT_SECRET"),
                oAuthConstants.AUTHORIZATION_CODE, code, GetAppSetting("FORGE_CALLBACK_URL"));

            dynamic credentialPublic = await oauth.RefreshtokenAsync(
                GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting("FORGE_CLIENT_SECRET"),
                "refresh_token", credentialInternal.refresh_token, new Scope[] { Scope.ViewablesRead });

            Credencial credencial = new Credencial();

            credencial.FORGE_CLIENT_ID = GetAppSetting("FORGE_CLIENT_ID");
            credencial.TokenInternal   = credentialInternal.access_token;
            credencial.TokenPublic     = credentialPublic.access_token;
            credencial.RefreshToken    = credentialPublic.refresh_token;
            credencial.ExpiresAt       = DateTime.Now.AddSeconds(credentialInternal.expires_in);

            //cookies.Append(FORGE_COOKIE, JsonConvert.SerializeObject(credentials));

            var client   = new MongoClient("mongodb://localhost:27017/auth_brass");
            var database = client.GetDatabase("auth_brass");

            var collection = database.GetCollection <BsonDocument>("credenciais");

            var list = await collection.Find(new BsonDocument("FORGE_CLIENT_ID", GetAppSetting("FORGE_CLIENT_ID"))).ToListAsync();

            if (list.Count > 0)
            {
                collection.DeleteOne(list[0]);
            }

            var ver = GetAppSetting("FORGE_CLIENT_ID");

            var bsonDocument = credencial.ToBsonDocument();
            await collection.InsertOneAsync(bsonDocument);

            return(credencial);
        }
        public static async Task <Credentials> CreateFromCodeAsync(string code)
        {
            ThreeLeggedApi oauth             = new ThreeLeggedApi();
            dynamic        credentialInteral = await oauth.GettokenAsync(
                Startup.Configuration["ForgeAPIID:FORGE_CLIENT_ID"], Startup.Configuration["ForgeAPIID:FORGE_CLIENT_SECRET"],
                oAuthConstants.AUTHORIZATION_CODE, code, Startup.Configuration["ForgeAPIID:FORGE_CALLBACK_URL"]);

            dynamic credentialPublic = await oauth.RefreshtokenAsync(
                Startup.Configuration["ForgeAPIID:FORGE_CLIENT_ID"], Startup.Configuration["ForgeAPIID:FORGE_CLIENT_SECRET"],
                "refresh_token", credentialInteral.refresh_token, new Scope[] { Scope.ViewablesRead });

            Credentials credentials = new Credentials
            {
                TokenInternal = credentialInteral.access_token,
                TokenPublic   = credentialPublic.access_token,
                RefreshToken  = credentialPublic.refresh_token,
                ExpiresAt     = DateTime.Now.AddSeconds(credentialInteral.expires_in)
            };

            _session.SetString("ForgeCredentials", JsonConvert.SerializeObject(credentials));
            return(credentials);
        }
        public async Task <bool> refreshToken()
        {
            Debug.Print("refreshToken : Refreshing token...");

            try
            {
                var authApi  = new ThreeLeggedApi();
                var response = await authApi.RefreshtokenAsync(
                    logInInfo.clientId, logInInfo.clientSecret, "refresh_token", logInInfo.refreshToken);

                logInInfo.accessToken  = response["access_token"];
                logInInfo.refreshToken = response["refresh_token"];
                logInInfo.expiresIn    = response["expires_in"];
            }
            catch (Exception ex)
            {
                Debug.Print("refreshToken >> catch : " + ex.Message);
                return(false);
            }

            return(true);
        }