/// <summary> /// Initializes a new instance of the Sentinel.OAuth.TokenManagers.RavenDbTokenRepository.Models.OAuth.RavenAuthorizationCode class. /// </summary> /// <param name="authorizationCode">The authorization code.</param> public RavenAuthorizationCode(IAuthorizationCode authorizationCode) : base(authorizationCode) { this.id = this.GenerateIdentifier(authorizationCode.ClientId, authorizationCode.RedirectUri, authorizationCode.Subject, authorizationCode.ValidTo); this.Created = DateTimeOffset.UtcNow; }
public async Task <bool> DeleteAuthorizationCode(IAuthorizationCode authorizationCode) { if (authorizationCode == null || string.IsNullOrEmpty(authorizationCode.Code)) { throw new ArgumentException("authorizationCode.Code MUST be set", "authorizationCode"); } return(await DeleteAuthorizationCode(authorizationCode.Code)); }
/// <summary>Tests if this IAuthorizationCode is considered equal to another.</summary> /// <param name="other">The i authorization code to compare to this object.</param> /// <returns>true if the objects are considered equal, false if they are not.</returns> public virtual bool Equals(IAuthorizationCode other) { if (this.GetIdentifier() == other.GetIdentifier()) { return(true); } return(false); }
/// <summary>Tests if this IAuthorizationCode is considered equal to another.</summary> /// <param name="other">The i authorization code to compare to this object.</param> /// <returns>true if the objects are considered equal, false if they are not.</returns> public bool Equals(IAuthorizationCode other) { if (this.ClientId == other.ClientId && this.RedirectUri == other.RedirectUri && this.Subject == other.Subject && this.ValidTo == other.ValidTo) { return true; } return false; }
/// <summary>Initializes a new instance of the Sentinel.OAuth.Core.Models.OAuth.AuthorizationCode class.</summary> /// <param name="authorizationCode">The authorization code.</param> public AuthorizationCode(IAuthorizationCode authorizationCode) { this.ClientId = authorizationCode.ClientId; this.RedirectUri = authorizationCode.RedirectUri; this.Subject = authorizationCode.Subject; this.Code = authorizationCode.Code; this.Scope = authorizationCode.Scope; this.Ticket = authorizationCode.Ticket; this.ValidTo = authorizationCode.ValidTo; }
/// <summary> /// Initializes a new instance of the Sentinel.OAuth.TokenManagers.SqlServerTokenRepository.Models.OAuth.SqlAuthorizationCode class. /// </summary> /// <param name="authorizationCode">The authorization code.</param> public SqlAuthorizationCode(IAuthorizationCode authorizationCode) : base(authorizationCode) { if (authorizationCode is SqlAuthorizationCode) { this.Id = ((SqlAuthorizationCode)authorizationCode).Id; } this.Created = DateTimeOffset.UtcNow; }
/// <summary>Inserts the specified authorization code.</summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns>The inserted authorization code. <c>null</c> if the insertion was unsuccessful.</returns> public async Task<IAuthorizationCode> InsertAuthorizationCode(IAuthorizationCode authorizationCode) { var code = (AuthorizationCode)authorizationCode; if (this.authorizationCodes.TryAdd(Guid.NewGuid(), code)) { return authorizationCode; } return null; }
public async Task <IAuthorizationCode> InsertAuthorizationCode(IAuthorizationCode authorizationCode) { var Service = StoreConstants.TokenServiceClient; var newToken = new AuthorizationCode(authorizationCode); newToken.IssuedAt = DateTimeOffset.UtcNow.DateTime; var uri = string.Format("api/oauth/token/authorizationcode/create"); var success = await Service.PostAsync <bool, AuthorizationCode>(uri, newToken); return(newToken); }
/// <summary> /// Inserts the specified authorization code. Called when creating an authorization code. /// </summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns> /// The inserted authorization code. <c>null</c> if the insertion was unsuccessful. /// </returns> public async Task<IAuthorizationCode> InsertAuthorizationCode(IAuthorizationCode authorizationCode) { var code = (RavenAuthorizationCode)authorizationCode; using (var session = this.OpenAsyncSession()) { await session.StoreAsync(code); await session.SaveChangesAsync(); return authorizationCode; } }
public async Task <IAuthorizationCode> CreateAuthorizationCodeAsync(IAuthorizationCode code) { var store = GetAuthorizationCodeStore(); // Delete all expired authorization codes await store.DeleteAuthorizationCodes(DateTimeOffset.UtcNow); // Add authorization code to database var insertResult = await store.InsertAuthorizationCode(code); return(insertResult); }
/// <summary>Deletes the specified authorization code.</summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns> public async Task <bool> DeleteAuthorizationCode(IAuthorizationCode authorizationCode) { var exists = this.authorizationCodes.Any(x => x.Key == authorizationCode.GetIdentifier()); if (exists) { AuthorizationCode removedCode; return(this.authorizationCodes.TryRemove(authorizationCode.GetIdentifier(), out removedCode)); } return(false); }
/// <summary>Deletes the specified authorization code.</summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns> public async Task <bool> DeleteAuthorizationCode(IAuthorizationCode authorizationCode) { using (var connection = await this.OpenConnection()) { var rows = await connection.ExecuteAsync( "DELETE FROM AuthorizationCodes WHERE Code = @Id", new { Id = authorizationCode.GetIdentifier() }); return(rows == 1); } }
/// <summary> /// Inserts the specified authorization code. Called when creating an authorization code. /// </summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns> /// The inserted authorization code. <c>null</c> if the insertion was unsuccessful. /// </returns> public async Task <IAuthorizationCode> InsertAuthorizationCode(IAuthorizationCode authorizationCode) { var code = new SqlAuthorizationCode(authorizationCode); if (!code.IsValid()) { throw new ArgumentException($"The authorization code is invalid: {JsonConvert.SerializeObject(code)}", nameof(authorizationCode)); } using (var connection = await this.OpenConnection()) { var id = await connection.QueryAsync <long>( "INSERT INTO AuthorizationCodes (ClientId, RedirectUri, Subject, Code, Scope, Ticket, ValidTo, Created) VALUES (@ClientId, @RedirectUri, @Subject, @Code, @Scope, @Ticket, @ValidTo, @Created); SELECT CAST(SCOPE_IDENTITY() as bigint);", new { code.ClientId, code.RedirectUri, code.Subject, code.Code, Scope = code.Scope != null ? string.Join(" ", code.Scope) : null, code.Ticket, code.ValidTo, Created = DateTimeOffset.UtcNow }); var data = await connection.QueryAsync("SELECT * FROM AuthorizationCodes WHERE Id = @Id", new { Id = id }); var entities = data.Select( x => new SqlAuthorizationCode() { ClientId = x.ClientId, Code = x.Code, Created = x.Created, Id = x.Id, RedirectUri = x.RedirectUri, Subject = x.Subject, Ticket = x.Ticket, ValidTo = x.ValidTo, Scope = x.Scope != null ? x.Scope.ToString().Split(' ') : new string[0] }); return(entities.FirstOrDefault()); } }
/// <summary>Deletes the specified authorization code.</summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns> public virtual async Task <bool> DeleteAuthorizationCode(IAuthorizationCode authorizationCode) { using (var session = this.OpenAsyncSession()) { var match = await session.Query <RavenAuthorizationCode>() .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite()) .Where(x => x.Code == authorizationCode.Code) .FirstOrDefaultAsync(); session.Delete(match); await session.SaveChangesAsync(); return(true); } }
/// <summary>Inserts the specified authorization code.</summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns>The inserted authorization code. <c>null</c> if the insertion was unsuccessful.</returns> public async Task <IAuthorizationCode> InsertAuthorizationCode(IAuthorizationCode authorizationCode) { var code = new AuthorizationCode(authorizationCode); if (!code.IsValid()) { throw new ArgumentException($"The authorization code is invalid: {JsonConvert.SerializeObject(code)}", nameof(authorizationCode)); } if (this.authorizationCodes.TryAdd(code.GetIdentifier().ToString(), code)) { return(code); } return(null); }
/// <summary>Deletes the specified authorization code.</summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns> public async Task <bool> DeleteAuthorizationCode(IAuthorizationCode authorizationCode) { var db = this.GetDatabase(); var tran = db.CreateTransaction(); var key = Convert.ToBase64String(Encoding.UTF8.GetBytes(authorizationCode.GetIdentifier())); // Remove items from indexes tran.SortedSetRemoveAsync($"{this.Configuration.AuthorizationCodePrefix}:_index:expires", $"{this.Configuration.AuthorizationCodePrefix}:{key}"); tran.HashDeleteAsync($"{this.Configuration.AuthorizationCodePrefix}:_index:redirecturi:{authorizationCode.RedirectUri}", key); // Remove key tran.KeyDeleteAsync($"{this.Configuration.AuthorizationCodePrefix}:{key}"); return(await tran.ExecuteAsync(CommandFlags.HighPriority)); }
/// <summary> /// Inserts the specified authorization code. Called when creating an authorization code. /// </summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns> /// The inserted authorization code. <c>null</c> if the insertion was unsuccessful. /// </returns> public async Task <IAuthorizationCode> InsertAuthorizationCode(IAuthorizationCode authorizationCode) { var code = new RedisAuthorizationCode(authorizationCode); if (!code.IsValid()) { throw new ArgumentException($"The authorization code is invalid: {JsonConvert.SerializeObject(code)}", nameof(authorizationCode)); } var key = this.GenerateKeyPath(code); var db = this.GetDatabase(); var tran = db.CreateTransaction(); try { this.Configuration.Log.DebugFormat("Inserting authorization code hash in key {0}", key); // Add hash to key tran.HashSetAsync(key, code.ToHashEntries()); var expires = code.ValidTo.ToUnixTime(); this.Configuration.Log.DebugFormat("Inserting key {0} to authorization code set with score {1}", key, expires); // Add key to sorted set for future reference. The score is the expire time in seconds since epoch. tran.SortedSetAddAsync($"{this.Configuration.AuthorizationCodePrefix}:_index:expires", key, expires); tran.HashSetAsync($"{this.Configuration.AuthorizationCodePrefix}:_index:redirecturi:{authorizationCode.RedirectUri}", key, expires); this.Configuration.Log.DebugFormat("Making key {0} expire at {1}", key, code.ValidTo); // Make the key expire when the code times out tran.KeyExpireAsync(key, code.ValidTo.UtcDateTime); await tran.ExecuteAsync(); return(code); } catch (Exception ex) { this.Configuration.Log.Error("Error when inserting authorization code", ex); } return(null); }
/// <summary> /// Inserts the specified authorization code. Called when creating an authorization code. /// </summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns> /// The inserted authorization code. <c>null</c> if the insertion was unsuccessful. /// </returns> public async Task<IAuthorizationCode> InsertAuthorizationCode(IAuthorizationCode authorizationCode) { var code = (RedisAuthorizationCode)authorizationCode; var key = this.GenerateKey(code); var db = this.GetDatabase(); try { if (code.ClientId == null || code.RedirectUri == null || code.Subject == null || code.Code == null || code.Ticket == null || code.Created == DateTime.MinValue || code.ValidTo == DateTime.MinValue) { throw new ArgumentException(string.Format("The authorization code is invalid: {0}", JsonConvert.SerializeObject(code)), "authorizationCode"); } this.Configuration.Log.DebugFormat("Inserting access token hash in key {0}", key); // Add hash to key await db.HashSetAsync(key, code.ToHashEntries()); var expires = authorizationCode.ValidTo.ToUnixTime(); this.Configuration.Log.DebugFormat("Inserting key {0} to authorization code set with score {1}", key, expires); // Add key to sorted set for future reference. The score is the expire time in seconds since epoch. await db.SortedSetAddAsync(this.Configuration.AuthorizationCodePrefix, key, expires); this.Configuration.Log.DebugFormat("Making key {0} expire at {1}", key, authorizationCode.ValidTo); // Make the key expire when the code times out await db.KeyExpireAsync(key, authorizationCode.ValidTo); return authorizationCode; } catch (Exception ex) { this.Configuration.Log.Error("Error when inserting authorization code", ex); } return null; }
/// <summary> /// Inserts the specified authorization code. Called when creating an authorization code. /// </summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns> /// The inserted authorization code. <c>null</c> if the insertion was unsuccessful. /// </returns> public virtual async Task <IAuthorizationCode> InsertAuthorizationCode(IAuthorizationCode authorizationCode) { var code = new RavenAuthorizationCode(authorizationCode); // Validate token if (!code.IsValid()) { throw new ArgumentException($"The authorization code is invalid: {JsonConvert.SerializeObject(code)}", nameof(authorizationCode)); } using (var session = this.OpenAsyncSession()) { await session.StoreAsync(code); await session.SaveChangesAsync(); return(code); } }
/// <summary> /// Inserts the specified authorization code. Called when creating an authorization code. /// </summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns> /// The inserted authorization code. <c>null</c> if the insertion was unsuccessful. /// </returns> public async Task<IAuthorizationCode> InsertAuthorizationCode(IAuthorizationCode authorizationCode) { using (var connection = this.OpenConnection()) { var id = await connection.QueryAsync<long>( "INSERT INTO AuthorizationCodes (ClientId, RedirectUri, Subject, Code, Scope, Ticket, ValidTo, Created) VALUES (@ClientId, @RedirectUri, @Subject, @Code, @Scope, @Ticket, @ValidTo, @Created); SELECT CAST(SCOPE_IDENTITY() as bigint);", new { authorizationCode.ClientId, authorizationCode.RedirectUri, authorizationCode.Subject, authorizationCode.Code, Scope = string.Join(" ", authorizationCode.Scope), authorizationCode.Ticket, authorizationCode.ValidTo, Created = DateTime.UtcNow }); var data = await connection.QueryAsync("SELECT * FROM AuthorizationCodes WHERE Id = @Id", new { Id = id }); var entities = data.Select( x => new SqlAuthorizationCode() { ClientId = x.ClientId, Code = x.Code, Created = x.Created, Id = x.Id, RedirectUri = x.RedirectUri, Subject = x.Subject, Ticket = x.Ticket, ValidTo = x.ValidTo, Scope = x.Scope != null ? x.Scope.ToString().Split(' ') : new string[0] }); return entities.FirstOrDefault(); } }
/// <summary> /// Deletes the specified authorization code. Called when authenticating an authorization code to /// prevent re-use. /// </summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns> public async Task<bool> DeleteAuthorizationCode(IAuthorizationCode authorizationCode) { var code = (RedisAuthorizationCode)authorizationCode; var key = this.GenerateKey(code); var db = this.GetDatabase(); // Remove items from set // We don't need to remove the keys themselves, as Redis will remove them for us because we set the EXPIRE parameter. return await db.SortedSetRemoveAsync(this.Configuration.AuthorizationCodePrefix, key); }
public async Task <bool> DeleteAuthorizationCode(IAuthorizationCode code) { var store = GetAuthorizationCodeStore(); return(await store.DeleteAuthorizationCode(code)); }
/// <summary> /// Deletes the specified authorization code. Called when authenticating an authorization code to /// prevent re-use. /// </summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns> public async Task<bool> DeleteAuthorizationCode(IAuthorizationCode authorizationCode) { var code = (RavenAuthorizationCode)authorizationCode; using (var session = this.OpenAsyncSession()) { var match = await session.LoadAsync<RavenAuthorizationCode>(code.Id); session.Delete(match); await session.SaveChangesAsync(); return true; } }
/// <summary>Deletes the specified authorization code.</summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns> public async Task<bool> DeleteAuthorizationCode(IAuthorizationCode authorizationCode) { var exists = this.authorizationCodes.Any(x => x.Value.Equals(authorizationCode)); if (exists) { var code = this.authorizationCodes.First(x => x.Value.Equals(authorizationCode)); AuthorizationCode removedCode; return this.authorizationCodes.TryRemove(code.Key, out removedCode); } return false; }
public RedisAuthorizationCode(IAuthorizationCode authorizationCode) : base(authorizationCode) { this.Created = DateTimeOffset.UtcNow; }
/// <summary> /// Deletes the specified authorization code. Called when authenticating an authorization code to /// prevent re-use. /// </summary> /// <param name="authorizationCode">The authorization code.</param> /// <returns><c>True</c> if successful, <c>false</c> otherwise.</returns> public async Task<bool> DeleteAuthorizationCode(IAuthorizationCode authorizationCode) { var code = (SqlAuthorizationCode)authorizationCode; using (var connection = this.OpenConnection()) { var rows = await connection.ExecuteAsync( "DELETE FROM AuthorizationCodes WHERE Id = @Id", new { Id = code.Id }); return rows == 1; } }