fake-d-oauth.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/sh
  2. # Fake `d-oauth-server` for the provision CLI-driver tests.
  3. #
  4. # State lives under $FAKE_STATE_DIR (a per-test tempdir). Every invocation is
  5. # appended to $FAKE_STATE_DIR/call.log as one argv line. Secrets are never
  6. # echoed: the only stdout token is the minted synthetic token, and tests assert
  7. # it never appears in any log.
  8. #
  9. # The `issue` step mirrors the real CLI wire shape: a human label plus the
  10. # token on ONE stdout line (`"<message>: <token>"`), never the bare token.
  11. # The token is a deterministic 44-char base64url-alphabet value
  12. # (`IATTEST` + zero-padded counter), never a real credential.
  13. #
  14. # Supported surface (argv-exact, no shell interpolation by the caller):
  15. # scope create <name>
  16. # user scopes --email <email>
  17. # user create --email <email> --password-file <path>
  18. # user grant-scope --email <email> --scope <scope>
  19. # redirect-uri create <uri> [--kind <kind>]
  20. # redirect-uri enable <uri> [--kind <kind>]
  21. # client attach-redirect-uri --client-id <id> --uri <uri> --kind <kind>
  22. # initial-access-token issue --uses <n> --ttl <secs>
  23. #
  24. # Failure hooks (env): FAKE_FAIL_STEP=grant forces `grant-scope` to fail as if
  25. # the scope were missing from the catalog.
  26. set -u
  27. S="${FAKE_STATE_DIR:?FAKE_STATE_DIR must be set}"
  28. echo "$*" >> "$S/call.log"
  29. fail() {
  30. echo "$1" >&2
  31. exit 1
  32. }
  33. case "${1:-}" in
  34. scope)
  35. # scope create <name>
  36. [ "${2:-}" = "create" ] || fail "usage error: scope $*"
  37. echo "$3" >> "$S/scopes.txt"
  38. echo "created scope $3"
  39. ;;
  40. user)
  41. case "${2:-}" in
  42. scopes)
  43. # user scopes --email <email>
  44. email="${4:-}"
  45. if grep -qx "$email" "$S/users.txt" 2>/dev/null; then
  46. echo "demo.read"
  47. else
  48. fail "not found: user $email"
  49. fi
  50. ;;
  51. create)
  52. # user create --email <email> --password-file <path>
  53. email="${4:-}"
  54. if grep -qx "$email" "$S/users.txt" 2>/dev/null; then
  55. fail "already exists: user $email"
  56. fi
  57. echo "$email" >> "$S/users.txt"
  58. echo "created user $email"
  59. ;;
  60. grant-scope)
  61. # user grant-scope --email <email> --scope <scope>
  62. email="${4:-}"
  63. scope="${6:-}"
  64. if [ "${FAKE_FAIL_STEP:-}" = "grant" ]; then
  65. fail "not found: scope $scope"
  66. fi
  67. if ! grep -qx "$scope" "$S/scopes.txt" 2>/dev/null; then
  68. fail "not found: scope $scope"
  69. fi
  70. echo "$email:$scope" >> "$S/grants.txt"
  71. echo "granted scope $scope to $email"
  72. ;;
  73. *)
  74. fail "usage error: user $*"
  75. ;;
  76. esac
  77. ;;
  78. redirect-uri)
  79. kind="authorize"
  80. [ "${4:-}" = "--kind" ] && kind="${5:-authorize}"
  81. case "${2:-}" in
  82. create)
  83. # redirect-uri create <uri> [--kind <kind>]
  84. echo "$3:$kind" >> "$S/redirects.txt"
  85. echo "created redirect $3 ($kind)"
  86. ;;
  87. enable)
  88. # redirect-uri enable <uri> [--kind <kind>]
  89. echo "$3:$kind" >> "$S/redirects-enabled.txt"
  90. echo "enabled redirect $3 ($kind)"
  91. ;;
  92. *)
  93. fail "usage error: redirect-uri $*"
  94. ;;
  95. esac
  96. ;;
  97. client)
  98. case "${2:-}" in
  99. attach-redirect-uri)
  100. # client attach-redirect-uri --client-id <id> --uri <uri> --kind <kind>
  101. echo "${4:-}:${6:-}:${8:-authorize}" >> "$S/attachments.txt"
  102. echo "attached ${6:-} (${8:-authorize}) to ${4:-}"
  103. ;;
  104. *)
  105. fail "usage error: client $*"
  106. ;;
  107. esac
  108. ;;
  109. initial-access-token)
  110. n="$(cat "$S/iat-counter.txt" 2>/dev/null || echo 0)"
  111. n=$((n + 1))
  112. echo "$n" > "$S/iat-counter.txt"
  113. token="$(printf 'IATTEST%037d' "$n")"
  114. echo "issued initial access token (1 use): $token"
  115. ;;
  116. *)
  117. fail "usage error: $*"
  118. ;;
  119. esac