Skip to content

Instantly share code, notes, and snippets.

@saelo
Created July 6, 2017 07:31
Show Gist options
  • Save saelo/091b1a92cc9b6ea9559d370990f888d7 to your computer and use it in GitHub Desktop.
Save saelo/091b1a92cc9b6ea9559d370990f888d7 to your computer and use it in GitHub Desktop.
Simple program to interact with authd via the macOS authorization API Raw
import Foundation
let rightname = "sys.openfile.readonly./tmp/cantread.txt"
var status: OSStatus
var authref: AuthorizationRef?
let flags = AuthorizationFlags([.interactionAllowed, .extendRights, .preAuthorize])
status = AuthorizationCreate(nil, nil, flags, &authref)
assert(status == errAuthorizationSuccess)
var item = AuthorizationItem(name: rightname, valueLength: 0, value: nil, flags: 0)
var rights = AuthorizationRights(count: 1, items: &item)
status = AuthorizationCopyRights(authref!, &rights, nil, flags, nil)
assert(status == errAuthorizationSuccess)
var token = AuthorizationExternalForm()
status = AuthorizationMakeExternalForm(authref!, &token)
assert(status == errAuthorizationSuccess)
let data = NSData(bytes: &token.bytes, length: kAuthorizationExternalFormLength)
data.write(toFile: "./token", atomically: true)
print("External form written to ./token")
print("Press enter to quit")
let _ = readLine()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment