1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-25 20:37:59 +01:00

Auto-adjust primaryUserId

This commit is contained in:
Paul Schaub 2024-02-03 13:32:30 +01:00
parent acd9566dbd
commit 9cea63529f
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -114,6 +114,12 @@ open class GenerateOpenPgpKey(
primaryFlags?.let { setKeyFlags(*it.toTypedArray()) } primaryFlags?.let { setKeyFlags(*it.toTypedArray()) }
} }
/**
* Add a direct-key signature to the primary key. If this method is called, the automatic
* addition of a direct-key signature in the final build-step will be omitted.
*
* @param subpacketsCallback callback to modify the subpackets of the direct-key signature.
*/
fun directKeySignature( fun directKeySignature(
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop() subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
) = apply { ) = apply {
@ -122,6 +128,10 @@ open class GenerateOpenPgpKey(
subpacketsCallback = preferencesCallback.then(subpacketsCallback)) subpacketsCallback = preferencesCallback.then(subpacketsCallback))
} }
/**
* If this method is called, the automatic addition of a direct-key signature in the final
* build-step will be omitted.
*/
fun noDirectKeySignature() = apply { addDirectKeySignature = false } fun noDirectKeySignature() = apply { addDirectKeySignature = false }
/** /**
@ -137,8 +147,24 @@ open class GenerateOpenPgpKey(
userId: CharSequence, userId: CharSequence,
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop() subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
) = apply { ) = apply {
val hasPrimaryUID =
primaryKey.key.publicKey.userIDs.asSequence().any { uid ->
primaryKey.key.publicKey.getSignaturesForID(uid).asSequence().any {
it.hashedSubPackets.isPrimaryUserID
}
}
val setPrimaryUID =
SelfSignatureSubpackets.applyHashed {
if (hasPrimaryUID) {
setPrimaryUserId(null)
} else {
setPrimaryUserId()
}
}
primaryKey.userId( primaryKey.userId(
userId, subpacketsCallback = preferencesCallback.then(subpacketsCallback)) userId,
subpacketsCallback =
preferencesCallback.then(setPrimaryUID).then(subpacketsCallback))
} }
/** /**