Skip to content

Move From oauth Blocks ​

Experimental

Connections are experimental and off by default. Turn them on with experimental_connections: true in config.yaml, or DAGU_EXPERIMENTAL_CONNECTIONS=true. See Turn Connections On.

An oauth block in a mail account or in smtp holds a refresh token in the DAG's secrets. Dagu never stores the new refresh token a provider returns there, so each copy of the block refreshes on its own and stops working when its token expires. A connection keeps one refresh token, saves each rotated one, and reports a revoked sign-in.

The oauth blocks keep working. They are deprecated, and the editor warns on each one.

The Editor Warning ​

While connections are on, the DAG editor warns on each oauth block in mail_accounts and smtp, and points to its replacement:

BlockWarning
google_refresh or microsoft_refreshMove its refresh token into a connection with dagu connection import, and name the connection with connection: instead
google_refresh in a provider: google mail accountThe same, and only when the token was granted https://mail.google.com/. See The Gmail API Caveat
microsoftName an oauth2 connection with the client credentials grant with connection: instead. It holds no refresh token to import
google_service_accountNone. No connection replaces it yet
  • An smtp block without host also says to set the host that the oauth block implied, since smtp with connection needs one.
  • The warnings cover the DAG's first YAML document only. A base config's blocks and a local sub-DAG's blocks get none.
  • A warning blocks no save.

Import a Refresh Token ​

dagu connection import creates a connected oauth2 connection from a refresh token, with no consent screen. It needs manager or above in the workspace.

  1. An admin registers the client that issued the token as an oauth2 app with a web or desktop client type: its authorization URL, token URL, client ID, and client secret, if it has one, as the oauth block names them. The import refuses an app without an authorization URL, which a reconnect uses. For Gmail, put access_type=offline in it, as Gmail shows. With --device-code, the app needs a public client and a device authorization URL instead.

    The import refreshes the token once. A provider that rotates refresh tokens then refuses the old one, so the oauth blocks that hold it fail until step 3 replaces them. Pause the DAGs that use the token until then.

  2. Import the token, naming the app, the scopes, and the mail servers as bound hosts:

bash
dagu connection import ops-mail --workspace ops --app mail-client \
  --scopes openid,email,https://mail.google.com/ \
  --bound-host imap.gmail.com --bound-host smtp.gmail.com \
  --refresh-token-stdin < token.txt
  1. Replace every oauth block that holds the token with connection:, and delete the token's secret:
yaml
labels:
  - workspace=ops

mail_accounts:
  ops@example.com:
    provider: google
    connection: ops-mail

steps:
  - id: find
    action: mail.search
    with:
      mailbox: ops@example.com
      within: 24h

Dagu fetches a token with the refresh token once. When the provider accepts it, the connection is created connected, with the granted scopes from the token response, and the CLI prints the result. The account comes from an ID token, which the provider returns only for the openid scope; without one, the CLI says the server names no account:

text
Imported connection ops-mail, signed in as ops@example.com.
Replace every oauth block that holds this refresh token with:
  connection: ops-mail
A copy left behind refreshes on its own and stops working when its token expires.
With a provider that revokes reused tokens, the two copies can break each other.
FlagMeaning
--appThe oauth2 app of the client that issued the token, by ID or name. Required
--scopesThe scopes the connection asks for at a reconnect, comma-separated. Required
--bound-hostA host that may receive the token. Repeat it, or give a comma-separated list. Required
--refresh-token-stdinRead the token from the first line of stdin. Without a terminal it is required. On a terminal, the CLI asks for the token without echo either way
--device-codeCreate a device code connection, which reconnects with a code. Needs an app with a public client and a device authorization URL
--workspaceThe workspace that owns the connection. Default: global

POST /connections/{name}/import does the same through the REST API. The Web UI has no import.

The Gmail API Caveat ​

A provider: google mail account with an oauth block reads mail through the Gmail API. The same account with connection reads it over IMAP and SMTP, which accept only a token with the https://mail.google.com/ scope. Import gives no new consent, so it cannot add that scope.

  • When the token was granted https://mail.google.com/, import it, and add --bound-host imap.gmail.com --bound-host smtp.gmail.com.
  • When it was granted only a Gmail API scope, such as https://www.googleapis.com/auth/gmail.modify, the import succeeds, but IMAP refuses the token. Add a new connection that asks for https://mail.google.com/ instead.

dagu connection show and the connection page list the granted scopes.

When an Import Is Not Possible ​

CaseWhat happensInstead
The provider refuses the token, as after a revocation or expiryThe import fails with the provider's error. Nothing is storedSign in with dagu connection add oauth2
The token fetch fails for a reason that may pass, such as HTTP 503The import fails. Nothing is storedRun the import again
A connection on this server holds the token, or was imported from itRefused, even after the provider rotated it, so two connections never share one grantSign in with dagu connection add oauth2. Some providers return the same refresh token to every consent of one account and client; see Refresh Token Limits
No oauth2 app for the token's client existsRefused. A client file cannot register one yetAsk an admin to register the app
microsoft or google_service_account blockNothing to import: neither holds a refresh tokenSee The Editor Warning
A Gmail API token without https://mail.google.com/Imported, but IMAP and SMTP refuse itSee The Gmail API Caveat

Imports run one at a time in each Dagu process. Two frontend replicas that import the same token at the same moment can both succeed, so import each token once.

Dagu is open source under the GNU General Public License v3.0.