Mark Pitt
claude-skills
  1. claude-skills
  2. API skills
  3. microsoft-graph-api
  4. resources/authentication-apps.md

A file in microsoft-graph-api

resources/authentication-apps.md

Markdown 13 KB
authentication-apps.mdMarkdown, rendered

Applications & Authentication - Microsoft Graph API

This resource covers app registrations, service principals, OAuth2 permissions, authentication methods, and credentials management in Azure AD.

Contents

Base Endpoints

  • Applications: https://graph.microsoft.com/v1.0/applications
  • Service Principals: https://graph.microsoft.com/v1.0/servicePrincipals
  • OAuth2 Permissions: https://graph.microsoft.com/v1.0/oauth2PermissionGrants
  • Authentication Methods: https://graph.microsoft.com/v1.0/users/{id}/authentication

Overview

Authentication in Microsoft Graph follows OAuth 2.0 standards:

  1. App registers in Azure AD
  2. User grants permissions (delegated) or app gets permissions (application)
  3. Access token obtained
  4. Token used in API calls

Applications

List Applications

GET /applications
GET /applications?$select=displayName,appId,createdDateTime

Get Application

GET /applications/{application-object-id}
GET /applications(appId='{client-id}')

Create Application

POST /applications
Content-Type: application/json

{
  "displayName": "My Application",
  "signInAudience": "AzureADMyOrg"
}

Required Permissions: Application.ReadWrite.All

signInAudience values:

  • AzureADMyOrg - Single tenant
  • AzureADMultipleOrgs - Multi-tenant
  • AzureADandPersonalMicrosoftAccount - Multi-tenant + personal accounts
  • PersonalMicrosoftAccount - Personal accounts only

Update Application

PATCH /applications/{id}
{
  "displayName": "Updated App Name",
  "web": {
    "redirectUris": [
      "https://myapp.com/callback"
    ]
  }
}

Delete Application

DELETE /applications/{id}

---

Application Properties

Configure Redirect URIs

PATCH /applications/{id}
{
  "web": {
    "redirectUris": [
      "https://myapp.com/auth/callback"
    ]
  },
  "spa": {
    "redirectUris": [
      "https://myapp.com/spa-callback"
    ]
  },
  "publicClient": {
    "redirectUris": [
      "https://login.microsoftonline.com/common/oauth2/nativeclient"
    ]
  }
}

Configure API Permissions

PATCH /applications/{id}
{
  "requiredResourceAccess": [
    {
      "resourceAppId": "00000003-0000-0000-c000-000000000000",
      "resourceAccess": [
        {
          "id": "e1fe6dd8-ba31-4d61-89e7-88639da4683d",
          "type": "Scope"
        },
        {
          "id": "df021288-bdef-4463-88db-98f22de89214",
          "type": "Role"
        }
      ]
    }
  ]
}

resourceAppId:

  • Microsoft Graph: 00000003-0000-0000-c000-000000000000

type:

  • Scope - Delegated permission
  • Role - Application permission

Expose API

PATCH /applications/{id}
{
  "identifierUris": [
    "api://{client-id}"
  ],
  "api": {
    "oauth2PermissionScopes": [
      {
        "adminConsentDescription": "Allow the app to access data",
        "adminConsentDisplayName": "Access data",
        "id": "{guid}",
        "isEnabled": true,
        "type": "User",
        "userConsentDescription": "Allow the app to access your data",
        "userConsentDisplayName": "Access your data",
        "value": "access_as_user"
      }
    ]
  }
}

Configure App Roles

PATCH /applications/{id}
{
  "appRoles": [
    {
      "allowedMemberTypes": ["User"],
      "description": "Administrators have full access",
      "displayName": "Administrator",
      "id": "{guid}",
      "isEnabled": true,
      "value": "Admin"
    },
    {
      "allowedMemberTypes": ["User"],
      "description": "Readers can view data",
      "displayName": "Reader",
      "id": "{guid}",
      "isEnabled": true,
      "value": "Reader"
    }
  ]
}

---

Service Principals

Service principals are the local representation of an application in a specific tenant.

List Service Principals

GET /servicePrincipals
GET /servicePrincipals?$filter=displayName eq 'My App'

Get Service Principal

GET /servicePrincipals/{id}
GET /servicePrincipals(appId='{client-id}')

Create Service Principal

POST /servicePrincipals
{
  "appId": "{client-id}"
}

Update Service Principal

PATCH /servicePrincipals/{id}
{
  "tags": ["WindowsAzureActiveDirectoryIntegratedApp"]
}

Delete Service Principal

DELETE /servicePrincipals/{id}

---

App Role Assignments

List App Role Assignments

GET /servicePrincipals/{sp-id}/appRoleAssignedTo

Assign App Role to User

POST /servicePrincipals/{sp-id}/appRoleAssignedTo
{
  "principalId": "{user-id}",
  "resourceId": "{sp-id}",
  "appRoleId": "{app-role-id}"
}

Assign App Role to Group

POST /groups/{group-id}/appRoleAssignments
{
  "principalId": "{group-id}",
  "resourceId": "{sp-id}",
  "appRoleId": "{app-role-id}"
}

Remove App Role Assignment

DELETE /servicePrincipals/{sp-id}/appRoleAssignedTo/{assignment-id}

---

OAuth2 Permission Grants

List Permission Grants

GET /oauth2PermissionGrants
GET /oauth2PermissionGrants?$filter=clientId eq '{sp-id}'

Grant Delegated Permissions

POST /oauth2PermissionGrants
{
  "clientId": "{client-sp-id}",
  "consentType": "AllPrincipals",
  "principalId": null,
  "resourceId": "{resource-sp-id}",
  "scope": "User.Read Mail.Read"
}

consentType:

  • AllPrincipals - Admin consent for all users
  • Principal - User consent for specific user (requires principalId)

Update Permission Grant

PATCH /oauth2PermissionGrants/{grant-id}
{
  "scope": "User.Read Mail.Read Calendars.Read"
}

Revoke Permission Grant

DELETE /oauth2PermissionGrants/{grant-id}

---

Application Credentials

Credentials secure your application authentication to Microsoft Graph.

List Password Credentials

GET /applications/{id}?$select=passwordCredentials

Add Password Credential (Client Secret)

POST /applications/{id}/addPassword
{
  "passwordCredential": {
    "displayName": "Client Secret 1"
  }
}

Returns: New secret value (store immediately - cannot retrieve later)

Remove Password Credential

POST /applications/{id}/removePassword
{
  "keyId": "{credential-key-id}"
}

List Certificate Credentials

GET /applications/{id}?$select=keyCredentials

Add Certificate Credential

POST /applications/{id}/addKey
{
  "keyCredential": {
    "type": "AsymmetricX509Cert",
    "usage": "Verify",
    "key": "BASE64_ENCODED_CERTIFICATE"
  },
  "passwordCredential": null,
  "proof": "{proof-token}"
}

---

Federated Identity Credentials

Use for workload identity federation (GitHub Actions, Kubernetes, etc.) - no secrets needed.

List Federated Credentials

GET /applications/{id}/federatedIdentityCredentials

Add Federated Credential

POST /applications/{id}/federatedIdentityCredentials
{
  "name": "GitHubActions",
  "issuer": "https://token.actions.githubusercontent.com",
  "subject": "repo:org/repo:environment:Production",
  "audiences": ["api://AzureADTokenExchange"]
}

---

Owners

List Application Owners

GET /applications/{id}/owners

Add Owner

POST /applications/{id}/owners/$ref
{
  "@odata.id": "https://graph.microsoft.com/v1.0/users/{user-id}"
}

Remove Owner

DELETE /applications/{id}/owners/{user-id}/$ref

---

App Templates

List Application Templates

GET /applicationTemplates

Get Template

GET /applicationTemplates/{template-id}

Instantiate Template

POST /applicationTemplates/{template-id}/instantiate
{
  "displayName": "My App from Template"
}

---

Policies

Token Lifetime Policies

List Policies
GET /policies/tokenLifetimePolicies
Create Policy
POST /policies/tokenLifetimePolicies
{
  "displayName": "Custom Token Lifetime",
  "definition": [
    "{\"TokenLifetimePolicy\":{\"Version\":1,\"AccessTokenLifetime\":\"4:00:00\"}}"
  ]
}
Assign Policy to Application
POST /applications/{app-id}/tokenLifetimePolicies/$ref
{
  "@odata.id": "https://graph.microsoft.com/v1.0/policies/tokenLifetimePolicies/{policy-id}"
}

Home Realm Discovery Policies

POST /policies/homeRealmDiscoveryPolicies
{
  "displayName": "HRD Policy",
  "definition": [
    "{\"HomeRealmDiscoveryPolicy\":{\"AccelerateToFederatedDomain\":true}}"
  ]
}

---

Authentication Methods

Authentication methods allow users to sign in.

List User's Authentication Methods

GET /users/{user-id}/authentication/methods

Phone Authentication

List Phone Methods
GET /users/{user-id}/authentication/phoneMethods
Add Phone Method
POST /users/{user-id}/authentication/phoneMethods
{
  "phoneNumber": "+1 555-0100",
  "phoneType": "mobile"
}

Phone types: mobile, alternateMobile, office

Email Authentication

Get Email Methods
GET /users/{user-id}/authentication/emailMethods
Add Email Method
POST /users/{user-id}/authentication/emailMethods
{
  "emailAddress": "backup@example.com"
}

FIDO2 Security Keys

List FIDO2 Methods
GET /users/{user-id}/authentication/fido2Methods

Microsoft Authenticator

List Authenticator Methods
GET /users/{user-id}/authentication/microsoftAuthenticatorMethods

Temporary Access Pass (TAP)

Create TAP
POST /users/{user-id}/authentication/temporaryAccessPassMethods
{
  "lifetimeInMinutes": 60,
  "isUsableOnce": true
}

Returns: One-time password for passwordless onboarding

Password Methods

Reset Password
POST /users/{user-id}/authentication/passwordMethods/{method-id}/resetPassword
{
  "newPassword": "NewP@ssw0rd!"
}

---

Application Extensions

Define Schema Extension

POST /schemaExtensions
{
  "id": "myapp_customData",
  "description": "Custom data for my app",
  "targetTypes": ["User"],
  "properties": [
    {
      "name": "customField",
      "type": "String"
    }
  ]
}

Use Extension

PATCH /users/{user-id}
{
  "myapp_customData": {
    "customField": "value"
  }
}

---

Synchronization (App Provisioning)

Get Synchronization Schema

GET /servicePrincipals/{sp-id}/synchronization/jobs/{job-id}/schema

Start Synchronization

POST /servicePrincipals/{sp-id}/synchronization/jobs/{job-id}/start

Pause Synchronization

POST /servicePrincipals/{sp-id}/synchronization/jobs/{job-id}/pause

---

Permissions Reference

Delegated Permissions

  • Application.Read.All - Read all applications
  • Application.ReadWrite.All - Read and write all applications
  • Application.ReadWrite.OwnedBy - Manage apps the user owns

Application Permissions

  • Application.Read.All - Read all applications
  • Application.ReadWrite.All - Read and write all applications
  • Application.ReadWrite.OwnedBy - Manage owned applications

---

Common Patterns

Register App with Permissions

# 1. Create application
POST /applications
{
  "displayName": "My App",
  "requiredResourceAccess": [...]
}

# 2. Create service principal
POST /servicePrincipals
{
  "appId": "{client-id}"
}

# 3. Grant admin consent
POST /oauth2PermissionGrants
{...}

Rotate Client Secret

# 1. Add new secret
POST /applications/{id}/addPassword
{...}

# 2. Update application to use new secret

# 3. Remove old secret
POST /applications/{id}/removePassword
{...}

Setup Federated Identity (GitHub Actions)

# 1. Register application
POST /applications
{...}

# 2. Add federated credential
POST /applications/{id}/federatedIdentityCredentials
{
  "name": "GitHubActions",
  "issuer": "https://token.actions.githubusercontent.com",
  "subject": "repo:org/repo:ref:refs/heads/main",
  "audiences": ["api://AzureADTokenExchange"]
}

# 3. Use in GitHub Actions workflow (no secrets needed)

---

Best Practices

  1. Use managed identities when possible (Azure resources)
  2. Rotate secrets regularly (every 90 days recommended)
  3. Use certificates instead of secrets for production
  4. Use federated identities for CI/CD (no secrets)
  5. Request least privilege permissions
  6. Use workload identity federation for external systems
  7. Monitor consent grants regularly
  8. Document custom app roles clearly
  9. Implement proper token caching
  10. Test multi-tenant apps in multiple tenants

---

Troubleshooting

IssueSolution
401 Invalid tokenCheck token expiration, request new token
403 Permission deniedVerify app has required permissions in Azure AD
App not foundVerify appId/application object ID is correct
Credential expiredRenew password/certificate credentials
Cannot add secretVerify you have Application.ReadWrite.All