-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split auth service into auth and state services
- Loading branch information
1 parent
52be21c
commit 1968c95
Showing
5 changed files
with
109 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
|
||
namespace SpotifyVue.Services | ||
{ | ||
public class AuthStateService | ||
{ | ||
/// A dictionary of state, userHash for verification of authorization callback | ||
/// IRL this would be Redis, Table Storage or Cosmos DB | ||
private readonly ConcurrentDictionary<string, string> _states = new ConcurrentDictionary<string, string>(); | ||
|
||
/// Creates and stores a new state value GUID | ||
public string NewState(string userId) | ||
{ | ||
// Store the state | ||
string state = Guid.NewGuid().ToString("N"); | ||
_states.TryAdd(state, userId); | ||
return state; | ||
} | ||
|
||
/// throws an exception if state is not found in dictionary, userId does not match | ||
public void ValidateState(string state, string userId) | ||
{ | ||
string value; | ||
if (!_states.TryGetValue(state, out value)) throw new InvalidOperationException("Invalid State value"); | ||
if (value != userId) throw new InvalidOperationException("Invalid State value"); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using SpotifyApi.NetCore; | ||
using SpotifyApi.NetCore.Authorization; | ||
using SpotifyVue.Models; | ||
|
||
namespace SpotifyVue.Services | ||
{ | ||
public class UserAuthService: IRefreshTokenProvider | ||
{ | ||
// IRL this would be Table / Cosmos Db | ||
private readonly ConcurrentDictionary<string, UserAuth> _userAuths = new ConcurrentDictionary<string, UserAuth>(); | ||
|
||
public UserAuth CreateUserAuth (string userId) | ||
{ | ||
// Create the User Auth | ||
var userAuth = GetUserAuth(userId); | ||
userAuth = userAuth ?? new UserAuth | ||
{ | ||
Authorized=false, | ||
UserId = userId | ||
}; | ||
|
||
InsertOrUpdateUserAuth(userId, userAuth); | ||
|
||
return userAuth; | ||
} | ||
|
||
public Task<string> GetRefreshToken(string userId) | ||
{ | ||
return Task.FromResult(GetUserAuth(userId).RefreshToken); | ||
} | ||
|
||
public UserAuth GetUserAuth(string userId) | ||
{ | ||
UserAuth value = null; | ||
_userAuths.TryGetValue(userId, out value); | ||
return value; | ||
} | ||
|
||
public UserAuth SetUserAuthRefreshToken(string userId, BearerAccessRefreshToken tokens) | ||
{ | ||
//TODO: No concurrency checking. Blows away any existing record | ||
var userAuth = GetUserAuth(userId); | ||
if (userAuth == null) throw new InvalidOperationException($"No valid User Auth record found for user hash \"{userId}\""); | ||
userAuth.Authorized = true; | ||
userAuth.RefreshToken = tokens.RefreshToken; | ||
userAuth.Scopes = tokens.Scope; | ||
InsertOrUpdateUserAuth(userId, userAuth); | ||
return userAuth; | ||
} | ||
|
||
private void InsertOrUpdateUserAuth(string userId, UserAuth userAuth) | ||
{ | ||
userAuth.EnforceInvariants(); | ||
_userAuths[userId] = userAuth; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters