Skip to content

Instantly share code, notes, and snippets.

@fuzzamos
Forked from saelo/authorize.swift
Created July 16, 2019 20:59
Show Gist options
  • Save fuzzamos/3bec3fde5d26c1ef90588ce18e6786b4 to your computer and use it in GitHub Desktop.
Save fuzzamos/3bec3fde5d26c1ef90588ce18e6786b4 to your computer and use it in GitHub Desktop.

Revisions

  1. @saelo saelo created this gist Jul 6, 2017.
    26 changes: 26 additions & 0 deletions authorize.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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()