Something went wrong. Try again.
An OIDC provider for AT Protocol OAuth. Rust port of apenwarr/atlogin.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259{ config, lib, pkgs, ...}:let cfg = config.services.ratlogin; groups = config.users.groups; users = config.users.users; serviceGroup = id: if users ? ${id} && !users.${id}.isNormalUser && users.${id}.group == id then id else null; clientGroup = client: if client.group != null then client.group else serviceGroup client.id; readers = group: lib.attrNames ( lib.filterAttrs ( _: user: user.isNormalUser && (user.group == group || lib.elem group user.extraGroups) ) users ); format = pkgs.formats.toml { }; stateDir = "/var/lib/ratlogin"; secretsDir = "/etc/ratlogin/clients"; secretPath = client: if client.secretFile != null then client.secretFile else "${secretsDir}/${client.id}"; generated = builtins.filter (client: client.secretFile == null) cfg.clients; generateSecrets = pkgs.writeShellScript "ratlogin-client-secrets" '' umask 077 mkdir -p ${secretsDir} chmod 0711 ${builtins.dirOf secretsDir} ${secretsDir} ${lib.concatMapStrings (client: '' if [ ! -s ${secretsDir}/${client.id} ]; then ${pkgs.openssl}/bin/openssl rand -hex 32 > ${secretsDir}/${client.id}.tmp mv ${secretsDir}/${client.id}.tmp ${secretsDir}/${client.id} fi chown root:${ if clientGroup client != null then clientGroup client else "root" } ${secretsDir}/${client.id} chmod ${if clientGroup client != null then "0640" else "0600"} ${secretsDir}/${client.id} '') generated} ''; configFile = format.generate "ratlogin.toml" ( { inherit (cfg) issuer listen; client_name = cfg.clientName; trust_proxy_headers = cfg.trustProxyHeaders; webfinger_domains = cfg.webfingerDomains; session_days = cfg.sessionDays; allow_private_fetch = cfg.allowPrivateFetch; rate_limit = { burst = cfg.rateLimit.burst; period_seconds = cfg.rateLimit.periodSeconds; }; clients = map ( client: { inherit (client) id email deny; secret_file = "/run/credentials/ratlogin.service/client-${client.id}"; redirect_uris = client.redirectUris; signing_algorithm = client.signingAlgorithm; insecure_redirects = client.insecureRedirects; } // lib.optionalAttrs (client.allow != null) { inherit (client) allow; } ) cfg.clients; } // lib.optionalAttrs (cfg.extraCss != null) { extra_css = cfg.extraCss; } ); exe = lib.getExe cfg.package; emailOption = lib.mkOption { type = lib.types.enum [ "none" "optional" "required" ]; default = "none"; }; signingOption = lib.mkOption { type = lib.types.enum [ "RS256" "ES256" ]; default = "ES256"; };in{ options.services.ratlogin = { enable = lib.mkEnableOption "ratlogin OpenID Connect provider"; package = lib.mkOption { type = lib.types.package; }; issuer = lib.mkOption { type = lib.types.str; example = "https://ratlogin.example.net"; }; listen = lib.mkOption { type = lib.types.str; default = "127.0.0.1:9411"; }; clientName = lib.mkOption { type = lib.types.str; default = "ratlogin"; }; logFilter = lib.mkOption { type = lib.types.str; default = "info"; }; sessionDays = lib.mkOption { type = lib.types.ints.between 0 365; default = 30; }; webfingerDomains = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ ]; }; extraCss = lib.mkOption { type = lib.types.nullOr lib.types.lines; default = null; description = "Extra CSS to inject into ratlogin HTML pages."; }; trustProxyHeaders = lib.mkOption { type = lib.types.bool; default = lib.any (prefix: lib.hasPrefix prefix cfg.listen) [ "127." "[::1]" "localhost" ]; }; allowPrivateFetch = lib.mkOption { type = lib.types.bool; default = false; }; rateLimit = { burst = lib.mkOption { type = lib.types.ints.positive; default = 30; }; periodSeconds = lib.mkOption { type = lib.types.ints.positive; default = 1; }; }; clients = lib.mkOption { default = [ ]; type = lib.types.listOf ( lib.types.submodule { options = { id = lib.mkOption { type = lib.types.str; }; secretFile = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; }; group = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; }; redirectUris = lib.mkOption { type = lib.types.listOf lib.types.str; }; email = emailOption; signingAlgorithm = signingOption; allow = lib.mkOption { type = lib.types.nullOr (lib.types.listOf lib.types.str); default = null; }; deny = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ ]; }; insecureRedirects = lib.mkOption { type = lib.types.bool; default = false; }; }; } ); }; };
config = lib.mkIf cfg.enable { assertions = map (client: { assertion = client.group == null || groups ? ${client.group}; message = "services.ratlogin: client ${client.id} names group ${toString client.group}, which this configuration does not declare"; }) cfg.clients;
warnings = lib.concatMap ( client: let group = serviceGroup client.id; people = if group == null then [ ] else readers group; in lib.optional (client.group == null && people != [ ]) "services.ratlogin: the secret for client ${client.id} is readable by ${lib.concatStringsSep ", " people} through group ${group}; set group explicitly to hide this warning" ) cfg.clients;
environment.systemPackages = [ cfg.package ];
users.users.ratlogin = { isSystemUser = true; group = "ratlogin"; }; users.groups.ratlogin = { };
systemd.services.ratlogin-client-secrets = lib.mkIf (generated != [ ]) { unitConfig.DefaultDependencies = false; after = [ "local-fs.target" ]; before = [ "sysinit.target" ]; wantedBy = [ "sysinit.target" ]; requiredBy = [ "ratlogin.service" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; ExecStart = generateSecrets; }; };
systemd.services.ratlogin = { description = "ratlogin OpenID Connect provider"; wantedBy = [ "multi-user.target" ]; after = [ "network-online.target" ]; wants = [ "network-online.target" ]; environment.RUST_LOG = cfg.logFilter; serviceConfig = { ExecStart = "${exe} serve --config ${configFile} --state-dir ${stateDir}"; LoadCredential = map (client: "client-${client.id}:${secretPath client}") cfg.clients; User = "ratlogin"; Group = "ratlogin"; StateDirectory = "ratlogin"; StateDirectoryMode = "0700"; Restart = "on-failure"; RestartSec = 5; NoNewPrivileges = true; PrivateTmp = true; PrivateDevices = true; ProtectSystem = "strict"; ProtectHome = true; ProtectClock = true; ProtectControlGroups = true; ProtectHostname = true; ProtectKernelLogs = true; ProtectKernelModules = true; ProtectKernelTunables = true; RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ]; RestrictNamespaces = true; RestrictRealtime = true; RestrictSUIDSGID = true; LockPersonality = true; MemoryDenyWriteExecute = true; CapabilityBoundingSet = ""; SystemCallArchitectures = "native"; SystemCallFilter = [ "@system-service" "~@privileged" ]; }; }; };}