-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/Contracts/Masa.Mc.Contracts.Admin/Infrastructure/TokenGenerater.cs
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,51 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the Apache License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.Mc.Contracts.Admin.Infrastructure; | ||
|
||
public class TokenGenerater : ITokenGenerater | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
private readonly HttpClient _httpClient; | ||
private readonly IMasaStackConfig _masaStackConfig; | ||
private const string SCHEME = "Bearer "; | ||
|
||
public TokenGenerater(IHttpContextAccessor httpContextAccessor, HttpClient httpClient, IMasaStackConfig masaStackConfig) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
_httpClient = httpClient; | ||
_masaStackConfig = masaStackConfig; | ||
} | ||
|
||
public TokenProvider Generater() | ||
{ | ||
StringValues authenticationHeaderValue; | ||
|
||
if (_httpContextAccessor.HttpContext?.Request.Headers.TryGetValue("Authorization", out authenticationHeaderValue) == true) | ||
{ | ||
var accessToken = authenticationHeaderValue.ToString(); | ||
|
||
if (!string.IsNullOrEmpty(accessToken) && accessToken.StartsWith(SCHEME, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
accessToken = accessToken.Substring(SCHEME.Length).Trim(); | ||
} | ||
|
||
return new TokenProvider { AccessToken = accessToken }; | ||
} | ||
|
||
if (_httpContextAccessor.HttpContext == null) | ||
{ | ||
var request = new ClientCredentialsTokenRequest | ||
{ | ||
Address = _masaStackConfig.GetSsoDomain() + "/connect/token", | ||
GrantType = BuildingBlocks.Authentication.OpenIdConnect.Models.Constans.GrantType.CLIENT_CREDENTIALS, | ||
ClientId = _masaStackConfig.GetWebId(MasaStackProject.MC), | ||
Scope = "MasaStack" | ||
}; | ||
var tokenResponse = _httpClient.RequestClientCredentialsTokenAsync(request).Result; | ||
return new TokenProvider { AccessToken = tokenResponse.AccessToken }; | ||
} | ||
|
||
return new TokenProvider(); | ||
} | ||
} |
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