-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UseOnceSecret and ConfigValue#useOnceSecret
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 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,41 @@ | ||
/* | ||
* Copyright 2017-2021 Viktor Lövgren | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
package ciris | ||
|
||
import cats.effect.kernel.{Resource, Sync} | ||
import cats.implicits._ | ||
import java.util.Arrays | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
trait UseOnceSecret { | ||
def resource[F[_]](implicit F: Sync[F]): Resource[F, Array[Char]] | ||
|
||
final def useOnce[F[_], A](f: Array[Char] => F[A])(implicit F: Sync[F]): F[A] = | ||
resource[F].use(f) | ||
} | ||
|
||
object UseOnceSecret { | ||
final def apply[F[_]](secret: Array[Char])(implicit F: Sync[F]): F[UseOnceSecret] = | ||
F.delay(new AtomicReference(secret.some)).map { ref => | ||
new UseOnceSecret { | ||
override final def resource[G[_]](implicit G: Sync[G]): Resource[G, Array[Char]] = { | ||
val acquire: G[Array[Char]] = | ||
G.delay(ref.getAndSet(None)).flatMap { | ||
case Some(secret) => | ||
G.pure(secret) | ||
case None => | ||
G.raiseError(new IllegalStateException("Secret has already been used once")) | ||
} | ||
|
||
val release: G[Unit] = | ||
G.delay(Arrays.fill(secret, ' ')) | ||
|
||
Resource.make(acquire)(_ => release) | ||
} | ||
} | ||
} | ||
} |