This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#22 APIキーの設定を追加
- Loading branch information
Showing
8 changed files
with
172 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace KoeBook.Core.Contracts.Services; | ||
|
||
public interface ISecretSettingsService | ||
{ | ||
Task<string?> GetApiKeyAsync(string folderPath, CancellationToken cancellationToken); | ||
|
||
Task SaveApiKeyAsync(string folderPath, string apiKey, CancellationToken cancellationToken); | ||
} |
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,49 @@ | ||
#pragma warning disable CA1416 // プラットフォームの互換性を検証 | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using KoeBook.Core.Contracts.Services; | ||
|
||
namespace KoeBook.Core.Services; | ||
|
||
public class SecretSettingsService : ISecretSettingsService | ||
{ | ||
private readonly byte[] _bytes; | ||
|
||
public SecretSettingsService() | ||
{ | ||
ulong value = 2546729043367843253ul; | ||
value ^= value << 13; | ||
value ^= value >> 7; | ||
value ^= value << 17; | ||
_bytes = BitConverter.GetBytes(value); | ||
} | ||
|
||
public Task<string?> GetApiKeyAsync(string folderPath, CancellationToken cancellationToken) | ||
{ | ||
return Task.Run(async () => | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
var data = await File.ReadAllBytesAsync(Path.Combine(folderPath, "alt"), cancellationToken).ConfigureAwait(false); | ||
if (data is null) | ||
return null; | ||
|
||
var result = ProtectedData.Unprotect(data, _bytes, DataProtectionScope.CurrentUser); | ||
return Encoding.UTF8.GetString(result); | ||
}, cancellationToken); | ||
} | ||
|
||
public Task SaveApiKeyAsync(string folderPath, string apiKey, CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(apiKey); | ||
return Task.Run(async () => | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
var data = Encoding.UTF8.GetBytes(apiKey); | ||
|
||
var result = ProtectedData.Protect(data, _bytes, DataProtectionScope.CurrentUser); | ||
if (!Directory.Exists(folderPath)) | ||
Directory.CreateDirectory(folderPath); | ||
await File.WriteAllBytesAsync(Path.Combine(folderPath, "alt"), result, cancellationToken).ConfigureAwait(false); | ||
}, cancellationToken); | ||
} | ||
} |
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
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
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