Skip to content

About

openconnect-saml in jennifer-lang

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

20 Commits

Folders and files

Repository files navigation

ocsaml

Connect to a Cisco AnyConnect gateway that authenticates with SAML/SSO, using your ordinary desktop browser and plain openconnect. Written in Jennifer.

No embedded webview, no Playwright: your real browser does the login, so Duo push, hardware keys and saved passwords work as they always do. With a Chromium-based browser the resulting cookie is collected for you.

ocsaml probe        # what does my gateway want?
ocsaml connect      # log in and bring the tunnel up
ocsaml status
ocsaml disconnect

Requirements

  • openconnect
  • jennifer >=0.27.0; the program declares pragma-jennifer-version: >=0.27.0
  • a browser; Chromium-based (chromium, chrome, brave, vivaldi, edge) also gets automatic cookie capture

Install

pacman

From a release:

sudo pacman -U ./ocsaml-*-any.pkg.tar.zst

makepkg

Builds the same package from the tree, running the suite first:

cd packaging/arch && makepkg -si

jvc

Into ~/.local, from the deck.toml in this repository:

jvc app install https://github.com/mplx/openconnect-saml

By hand

Keep bin/ and src/ adjacent (bin/ imports ../src/saml.j; a symlink is fine, Jennifer resolves imports against the real path):

install -Dm755 bin/ocsaml ~/.local/lib/ocsaml/bin/ocsaml
install -Dm644 -t ~/.local/lib/ocsaml/src src/*.j
ln -sf ../lib/ocsaml/bin/ocsaml ~/.local/bin/ocsaml

Then a profile, unless you pass --server every time:

install -Dm600 config.example.toml ~/.config/ocsaml/config.toml

Run it as your desktop user, never under sudo: it escalates for the tunnel on its own, and a Chromium-based browser will not start as root.

Configuration

~/.config/ocsaml/config.toml, or --server on the command line. Every top-level table is a profile; a default key picks one.

default = "work"

[work]
server  = "https://vpn.example.com/saml"   # group URL, path and all
browser = "chromium"

Put the whole group URL in server. On a Cisco gateway the URL path selects the tunnel group (it becomes <group-access>); --group (<group-select>) is widely ignored. probe prints the group the gateway resolved to, so a wrong path is visible at a glance.

key
server gateway URL including the group path
browser command that opens a URL; Chromium-based enables capture. - to print only
sudo privilege helper for the tunnel ("" if already root)
capture auto (default) / cdp / paste
cdpPort, browserProfile, captureTimeout capture browser knobs
user, passwordCommand, factor browserless mode
servercert pin the certificate yourself instead of using the gateway's
insecure, version, extra, group TLS opt-out, claimed client version, extra openconnect flags, <group-select>

Commands

connect [profile] authenticate, then start the tunnel in the background
cookie [profile] authenticate and print COOKIE / CONNECT_URL / FINGERPRINT
probe [profile] report how the gateway authenticates
status [profile] is the tunnel up? (exit 0/1)
disconnect [profile] stop a tunnel started by connect

Flags: --server, --group, --browser, --sudo, --mode, --capture, --user, --password-command, --factor, --debug-dir, --print, -v, -k. A usage mistake exits 2; --help works per subcommand.

How it works

connect asks the gateway which shape of SAML it speaks.

external - the gateway is configured with saml external-browser, which openconnect implements natively. All this adds is running openconnect --authenticate as you, so the browser opens in your session, then starting only the tunnel under sudo with the cookie on stdin.

token - the classic flow, where the token arrives as a cookie on the gateway's domain. This speaks the <config-auth> XML itself: POST init, read back the login URL and the gateway's <opaque> blob, collect the cookie, POST auth-reply with the blob echoed verbatim, then hand openconnect the <session-token> and the certificate hash the gateway reported.

Collecting the cookie

Set a Chromium-based browser and there is nothing to paste: it is launched with --remote-debugging-port, you log in normally, and the token is read out over the DevTools protocol as soon as the gateway sets it. This is the only way to get it automatically - the cookie is HttpOnly, so no page script or bookmarklet can see it.

The capture browser uses its own persistent profile (~/.cache/ocsaml/browser): separate so it never collides with a browser you have open, persistent so the IdP session and any "remember this device" survive to the next connect.

Otherwise (Firefox, or capture = "paste") you are given the login URL and asked for the cookie - F12 -> Application/Storage -> Cookies. The prompt takes the bare value, a name=value pair, a whole Cookie: header carrying that cookie, or the final URL.

Browserless mode

ocsaml connect --mode browserless --user jdoe

Opt-in, no browser: the IdP's HTML forms are walked directly until the gateway sets its cookie. Only works where the login is plain forms - a second factor or a JavaScript page ends it, naming what it hit. Duo is recognised by name.

The password comes from passwordCommand (e.g. pass show vpn/work), else $OCSAML_PASSWORD, else a hidden prompt. It is never read from the config file.

Two deliberate refusals: it never resubmits credentials (a second credential form means the first was rejected - retrying is how accounts get locked), and it never guesses at a form it does not understand.

Where the IdP offers factors as a form, --factor passcode picks one; the name is matched case-insensitively as a substring of each control's name, value and id. Without it the first control is taken. A real choice with no match is an error listing the options, not a guess.

Failures name the page, the fields found and the trail of URLs walked. -v shows each step; --debug-dir DIR saves every page (they contain CSRF tokens and SAML assertions, so the directory is created 0700 and the files 0600).

Notes

  • sudo. connect uses sudo -n, because the child has no terminal to prompt on. Run sudo -v first, or --print to get the command and run it yourself. connect checks this before it opens the browser, so a missing sudo -v costs no login, and again before it starts the tunnel, in case the cached credentials expired during the login.
  • Backgrounding. openconnect daemonises only once the tunnel is up, so a successful connect means connected. Pid in /run/ocsaml-<profile>.pid, output in ...log - read that first when a tunnel drops. The redirect is load-bearing: a daemon holding the inherited pipes would never let the launching call return.
  • The cookie never reaches the process list - it goes over stdin.
  • Certificate pinning is automatic: the gateway reports its own hash at the end of the login, and that becomes --servercert.
  • TLS is not optional. A plain http:// gateway is refused unless the host is loopback or --insecure is given. Browserless mode refuses --insecure outright and refuses any non-https hop: the form's action decides where the password goes. It prints the host that receives it.
  • disconnect confirms the pid is still openconnect (/proc/<pid>/cmdline) before signalling it as root. Pid files outlive tunnels; Linux reuses pids.
  • The capture browser is cleaned up on every exit path, timeout included, so no live SSO session is left behind an open debug port. That port carries no --remote-allow-origins: * would let any page in any local browser drive it and read the HttpOnly cookie.
  • The debug port is loopback-only and open for the length of the login. It has no origin check of its own, so on a machine shared with other users another local account could attach to it while you log in. --remote-debugging-pipe would avoid the socket, but it needs inherited file descriptors the interpreter cannot hand a child. On a shared machine, set capture = "paste".
  • Some gateways answer <error id="96">VPN Server internal error.</error> to a client that merely advertises external-browser support. That is treated as a no, and probe reports it.

Layout and tests

bin/ocsaml executable; imports src/saml.j, exits with its status
src/saml.j entry point - a facade over commands.j
src/types.j the structs and constants every module shares
src/util.j errors, escaping, shell quoting, byte and string shaping
src/config.j config file plus command line, resolved into one Profile
src/protocol.j the <config-auth> exchange
src/browser.j SSO token out of a browser: DevTools capture, or paste
src/tunnel.j driving openconnect, the pid file, the privilege helper
src/browserless.j the HTML form walker
src/commands.j the subcommands, the flags, run()
src/<topic>_test.j one white-box overlay per module, spliced by jennifer test
tests/run.sh the whole suite; what CI and makepkg run
tests/integration.sh end-to-end tests against tests/mock-gateway.j
tests/check-versions.sh, tests/check-docs.j version and doc-comment gates
deck.toml the app manifest jvc app install reads
packaging/arch/PKGBUILD the Arch package
tests/run.sh              # parse, format, lint, docs, versions, unit, integration
tests/run.sh 0.4.0        # ... and require that version, as a tag build does

The logic sits in src/ rather than the executable so each module's <topic>_test.j can splice it and reach the private surface. saml.j stays the entry point because bin/ and the installed layout both name that path, so the split is invisible from outside. Topic modules depend one way only: types <- util <- config / protocol / browser / tunnel / browserless <- commands. The PKGBUILD sits in packaging/arch/ because makepkg builds in $startdir/src and makepkg -C empties it - at the root that would be this project's own src/. The mock gateway is Jennifer, and the integration tests stub openconnect, so the suite needs no VPN, browser or root.

Troubleshooting

Start with ocsaml probe -v.

VPN Server internal error. wrong URL path, so wrong tunnel group - check probe's tunnel group line
offered no <sso-v2-login> URL that group is not using SAML
expected an auth-request ... wrong URL, or a portal in front of the gateway
did not complete the login token rejected; it expires quickly, so retry promptly
browser never opened its debug port another browser holds that profile or port - change cdpPort
browser exited without opening its debug port the browser's own stderr is quoted underneath; that line is the reason
certificate complaints from openconnect copy the pin-sha256: value it prints into servercert

About

openconnect-saml in jennifer-lang

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages