Skip to content

Commit

Permalink
Allow to show custom prompt in auth dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikPalo committed Jan 10, 2024
1 parent dacf5b6 commit 1cb1948
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
49 changes: 40 additions & 9 deletions Sources/Authorization/Authorization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ public struct Authorization {

/// Creates an authorizartion allowing to use specified executabale tools with admin privileges.
/// - Parameter pathsToTools: A set containing paths to tools for which we require authorization.
/// - Parameter prompt: A custom prompt (text) displayed in the authorization dialog instead of the system one.
/// - Returns: An opaque reference to an authorization object authorized to the specified tools or an error.
public static func authorize(
pathsToTools: Set<String>
pathsToTools: Set<String>,
prompt: String? = nil
) -> Result<AuthorizationRef, AuthorizationError> {
var authorizationRef: AuthorizationRef? = nil
var err = AuthorizationCreate(nil, nil, [], &authorizationRef)
Expand All @@ -22,7 +24,7 @@ public struct Authorization {
name.withUnsafeBufferPointer { nameBuf in
path.withUnsafeBufferPointer { pathBuf in
let pathPtr = UnsafeMutableRawPointer(mutating: pathBuf.baseAddress!)
return AuthorizationItem(
return AuthorizationItem(
name: nameBuf.baseAddress!,
valueLength: path.count,
value: pathPtr,
Expand All @@ -44,13 +46,42 @@ public struct Authorization {
.extendRights,
]

err = AuthorizationCopyRights(
authorizationRef!,
&rights,
nil,
flags,
nil
)
if let prompt {
let name = kAuthorizationEnvironmentPrompt.cString(using: .utf8)!
let promptCString = prompt.cString(using: .utf8)!

var promptItem = name.withUnsafeBufferPointer { nameBuf in
promptCString.withUnsafeBufferPointer { promptBuf in
AuthorizationItem(
name: nameBuf.baseAddress!,
valueLength: promptCString.count,
value: UnsafeMutableRawPointer(mutating: promptBuf.baseAddress!),
flags: 0
)
}
}

var environment = withUnsafeMutablePointer(to: &promptItem) { promptItemPtr in
AuthorizationEnvironment(count: 1, items: promptItemPtr)
}

err = AuthorizationCopyRights(
authorizationRef!,
&rights,
&environment,
flags,
nil
)
} else {
err = AuthorizationCopyRights(
authorizationRef!,
&rights,
nil,
flags,
nil
)
}

guard err == errAuthorizationSuccess else {
return .failure(.copyRights(err))
}
Expand Down
7 changes: 7 additions & 0 deletions Tests/AuthorizationTests/AuthorizationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@
let fh = try Authorization.executeWithPrivileges("/bin/ls /").get()
print(String(bytes: fh.readDataToEndOfFile(), encoding: .utf8)!)
}

func testAuthorizeWithCustomPrompt() throws {
throw XCTSkip("user required")

let authorization = try Authorization.authorize(pathsToTools: ["/bin/ls"], prompt: "Custom prompt for testing.").get()
AuthorizationFree(authorization, [.destroyRights])
}
}

0 comments on commit 1cb1948

Please sign in to comment.