%3CLINGO-SUB%20id%3D%22lingo-sub-1717430%22%20slang%3D%22en-US%22%3EUse%20MITREid%20Connect%20for%20OAuth2%20Authorization%20in%20API%20Management%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1717430%22%20slang%3D%22en-US%22%3E%3CH1%20id%3D%22use-mitreid-connect-for-oauth2-authorization-in-api-management%22%20class%3D%22code-line%22%20data-line%3D%220%22%20id%3D%22toc-hId-554114770%22%20id%3D%22toc-hId-554114769%22%3EUse%20MITREid%20Connect%20for%20OAuth2%20Authorization%20in%20API%20Management%3C%2FH1%3E%0A%3CH2%20class%3D%22code-line%22%20data-line%3D%222%22%20id%3D%22toc-hId-1244676244%22%20id%3D%22toc-hId-1244676243%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CP%3EBy%20(alphabetically)%3A%20Akinlolu%20Akindele%2C%20Dan%20Balma%2C%20Maarten%20Van%20De%20Bospoort%2C%20Erin%20Corson%2C%20Nick%20Drouin%2C%20Heba%20Elayoty%2C%20Andrei%20Ermilov%2C%20David%20Giard%2C%20Michael%20Green%2C%20Alfredo%20Chavez%20Hernandez%2C%20Hao%20Luo%2C%20Maggie%20Marxen%2C%20Siva%20Mullapudi%2C%20Nsikan%20Udoyen%2C%20William%20Zhang%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22introduction%22%20class%3D%22code-line%22%20data-line%3D%222%22%20id%3D%22toc-hId--562778219%22%20id%3D%22toc-hId--562778220%22%3EIntroduction%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%224%22%3EUsing%20an%20API%20gateway%20in%20front%20of%20REST%20APIs%20is%20a%20common%20design%20pattern%20which%20allows%20us%20to%20offload%20the%20cross-cutting%20capabilities%20such%20as%20OAuth2%20authorization%20to%20the%20gateway%20instead%20of%20letting%20security%20code%20get%20scattered%20in%20application%20code.%20Azure%20API%20Management%20(APIM)%20is%20such%20an%20API%20gateway%20service.%20For%20the%20common%20case%20that%20the%20OAuth2%20server%20in%20APIM%20is%20Azure%20AD%20(AAD)%2C%20it%20has%20been%20well%20documented.%20In%20this%20document%20we%20will%20cover%20the%20case%20that%20the%20OAuth2%20server%20is%20MITREid%20Connect%20instead%20of%20AAD.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%224%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%0A%3CDIV%3E%3CSPAN%3EThe%26nbsp%3Bfollowing%26nbsp%3Bdiagram%26nbsp%3Billustrates%26nbsp%3Bsuch%26nbsp%3Ba%26nbsp%3Bscenario%3A%26nbsp%3Bthe%26nbsp%3Bbackend%26nbsp%3BREST%26nbsp%3BAPI%26nbsp%3Bruns%26nbsp%3Bon%26nbsp%3Ban%26nbsp%3BAzure%26nbsp%3BKubernetes%26nbsp%3BService%26nbsp%3B(AKS)%26nbsp%3Bcluster.%26nbsp%3BWhile%26nbsp%3Bthe%26nbsp%3BAPI%26nbsp%3BManagement%20endpoint%26nbsp%3Bis%26nbsp%3Bsecured%26nbsp%3Bby%26nbsp%3BOAuth2%26nbsp%3Bauthorization%2C%26nbsp%3Bthe%26nbsp%3BAKS%26nbsp%3Bendpoint%26nbsp%3Bis%26nbsp%3Bsecured%26nbsp%3Bby%26nbsp%3Bmutual%26nbsp%3BTLS%26nbsp%3B(mTLS)%26nbsp%3Bbetween%26nbsp%3BAPIM%26nbsp%3Band%26nbsp%3BAKS.%26nbsp%3BThe%26nbsp%3Bsetup%26nbsp%3Bof%26nbsp%3BmTLS%26nbsp%3Bbetween%26nbsp%3BAKS%26nbsp%3Band%26nbsp%3BAPIM%26nbsp%3Bis%26nbsp%3Bcovered%26nbsp%3Bby%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-paas-blog%2Fmtls-between-aks-and-apim%2Fba-p%2F1813887%22%20target%3D%22_self%22%3Ea%26nbsp%3Bsister%26nbsp%3Bdocument%3C%2FA%3E.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22security_oauth2.drawio.png%22%20style%3D%22width%3A%20780px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F227095iF3A7645C357E7042%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22security_oauth2.drawio.png%22%20alt%3D%22security_oauth2.drawio.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%226%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%226%22%3E%3CA%20title%3D%22https%3A%2F%2Fkit.mit.edu%2Fprojects%2Fmitreid-connect%22%20href%3D%22https%3A%2F%2Fkit.mit.edu%2Fprojects%2Fmitreid-connect%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Fkit.mit.edu%2Fprojects%2Fmitreid-connect%22%3EMITREid%20Connect%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eis%20an%20open%20source%20Identity%20Provider%2C%20popular%20in%20Java%20community.%20MITREid%20Connect%20is%20compliant%20to%20OpenID%20Connect%20and%20OAuth%202.0%20protocol.%20This%20document%20is%20based%20on%20MITREid%20Connect%20v%201.3.3.%3C%2FP%3E%0A%3CH2%20class%3D%22code-line%22%20data-line%3D%228%22%20id%3D%22toc-hId-1924734614%22%20id%3D%22toc-hId-1924734613%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22prerequisites%22%20class%3D%22code-line%22%20data-line%3D%228%22%20id%3D%22toc-hId-117280151%22%20id%3D%22toc-hId-117280150%22%3EPrerequisites%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2210%22%3EAn%20Azure%20subscription%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2211%22%3EA%20MITREid%20Connect%20instance.%20This%20document%20focuses%20on%20how%20to%20enable%20OAuth2%20over%20APIM%20and%20MITREid%20Connect%2C%20instead%20of%20on%20how%20to%20set%20up%20MITREid%20Connect.%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2212%22%3EA%20REST%20API%20app%20for%20test.%20The%20REST%20API%20could%20be%20deployed%20to%20AKS%2C%20App%20Service%2C%20or%20other.%20Or%20use%20this%20sample%20REST%20API%20with%20Swagger%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22http%3A%2F%2Fconferenceapi.azurewebsites.net%2F%3Fformat%3Djson%22%20href%3D%22http%3A%2F%2Fconferenceapi.azurewebsites.net%2F%3Fformat%3Djson%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22http%3A%2F%2Fconferenceapi.azurewebsites.net%2F%3Fformat%3Djson%22%3Ehttp%3A%2F%2Fconferenceapi.azurewebsites.net%2F%3Fformat%3Djson%3C%2FA%3E.%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2213%22%3EFamiliar%20with%20Azure%20API%20Management%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2214%22%3EBasic%20understanding%20of%20OAuth2.%20OAuth2%20involves%20a%20few%20specs%20(OAuth2%2C%20OIDC%2C%20PKCE%20and%20JOSE).%20A%20list%20of%20good%20references%20on%20OAuth2%20and%20related%20specs%20can%20be%20found%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22https%3A%2F%2Foauth.net%2F2%2F%22%20href%3D%22https%3A%2F%2Foauth.net%2F2%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Foauth.net%2F2%2F%22%3Ehere%3C%2FA%3E.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2216%22%3ENOTE%3A%20This%20article%20does%20not%20cover%20the%20security%20between%20APIM%20and%20the%20REST%20API%20which%20is%20a%20separate%20topic.%20If%20a%20REST%20API%20is%20deployed%20to%20AKS%2C%20there%20are%20a%20few%20options%20which%20have%20been%20documented%20in%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fapi-management-kubernetes%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fapi-management-kubernetes%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fapi-management-kubernetes%22%3EUse%20Azure%20API%20Management%20with%20microservices%20deployed%20in%20Azure%20Kubernetes%20Service%3C%2FA%3E.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2218%22%3EIf%20APIM%20Premium%20is%20used%2C%20we%20can%20leverage%20VNET%20so%20that%20the%20security%20between%20APIM%20and%20AKS%20is%20simpler.%20Otherwise%2C%20we%20need%20to%20secure%20the%20REST%20calls%20from%20APIM%20to%20AKS%20thru%20technique%20such%20as%20mutual%20TLS%20(mTLS)%20(note%20that%20it%20is%20mutual%20TLS%2C%20not%20just%20TLS).%20Please%20see%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-paas-blog%2Fmtls-between-aks-and-apim%2Fba-p%2F1813887%22%20target%3D%22_self%22%3Ethis%20sister%20document%3C%2FA%3E.%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2218%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22preparations-in-mitreid-connect%22%20class%3D%22code-line%22%20data-line%3D%2220%22%20id%3D%22toc-hId--1690174312%22%20id%3D%22toc-hId--1690174313%22%3EPreparations%20in%20MITREid%20Connect%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2222%22%3EIn%20order%20to%20prepare%20for%20OAuth2%20setup%20in%20APIM%2C%20we%20need%20to%20perform%20the%20following%20steps%20in%20MITREid%20Connect%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2224%22%3ERegister%20a%20server%20application%20registration%3B%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2225%22%3EDefine%20its%20scopes%3B%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2226%22%3ERegister%20a%20client%20application%3B%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2227%22%3ESpecify%20its%20permissions%3A%20either%20Delegated%20Permissions%20or%20Application%20Permissions%3B%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2228%22%3ECreate%20a%20client%20secret.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2230%22%3EThe%20concepts%20and%20the%20steps%20for%20the%20above%20OAuth2%20steps%20between%20MITREid%20Connect%20and%20Azure%20AD%20are%20not%20fundamentally%20different.%20You%20may%20get%20detailed%20steps%20from%26nbsp%3B%3CA%20title%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fapi-management-howto-protect-backend-with-aad%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fapi-management-howto-protect-backend-with-aad%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fapi-management-howto-protect-backend-with-aad%22%3EProtect%20an%20API%20by%20using%20OAuth%202.0%20with%20Azure%20Active%20Directory%20and%20API%20Management%3C%2FA%3E%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2235%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2235%22%3EThe%20following%20parameters%20from%20MITREid%20Connect%20must%20be%20prepared%20and%20available%20for%20our%20setup%20in%20APIM.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2235%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3EParameter%3C%2FTH%3E%0A%3CTH%3ETypical%20Format%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3EClient%20ID%3C%2FTD%3E%0A%3CTD%3EA%20string.%20Unlike%20Azure%20AD%2C%20a%20client%20ID%20in%20MITREid%20Connect%20does%20not%20have%20to%20be%20a%20GUID.%20But%20it%20needs%20to%20be%20unique%20within%20its%20tenant.%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EClient%20secret%3C%2FTD%3E%0A%3CTD%3EHexadecimal%20representation%20of%20a%20cryptographically%20secure%20pseudorandom%20number%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EOpenID%20config%20endpoint%3C%2FTD%3E%0A%3CTD%3Ehttps%3A%2F%2F%5B%3CEM%3Ehost%3C%2FEM%3E%5D%2F.well-known%2Fopenid-configuration%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EAuthorization%20endpoint%3C%2FTD%3E%0A%3CTD%3Ehttps%3A%2F%2F%5B%3CEM%3Ehost%3C%2FEM%3E%5D%2Fauthorize%2F%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EToken%20endpoint%3C%2FTD%3E%0A%3CTD%3Ehttps%3A%2F%2F%5B%3CEM%3Ehost%3C%2FEM%3E%5D%2Ftoken%2F%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EAudience%3C%2FTD%3E%0A%3CTD%3EA%20string%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EIssuer%3C%2FTD%3E%0A%3CTD%3Ehttps%3A%2F%2F%5B%3CEM%3Ehost%3C%2FEM%3E%5D%2F%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EDefault%20Scopes%3C%2FTD%3E%0A%3CTD%3EOptional%2C%20and%20it%20could%20have%20multiple%20values%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CH2%20class%3D%22code-line%22%20data-line%3D%2249%22%20id%3D%22toc-hId-797338521%22%20id%3D%22toc-hId-797338520%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22create-an-oauth2-server-on-mitreid-connect%22%20class%3D%22code-line%22%20data-line%3D%2249%22%20id%3D%22toc-hId--1010115942%22%20id%3D%22toc-hId--1010115943%22%3ECreate%20an%20OAuth2%20Server%20on%20MITREid%20Connect%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2251%22%3EIn%20Azure%20portal%20left%20menu%20column%2C%20click%20%22OAuth%202.0%22%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2252%22%3EClick%20%22Add%22%20button%20on%20the%20right%20pane%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2253%22%3EYou%20need%20the%20following%20parameters%20to%20create%20the%20OAuth%202%20server%20on%20MITREid%20Connect%3A%3CUL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2254%22%3EClient%20ID%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2255%22%3EClient%20secret%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2256%22%3EAuthorization%20endpoint%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2257%22%3EToken%20endpoint%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2259%22%3EFor%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EAuthorization%20grant%20types%3C%2FEM%3E%2C%20you%20may%20choose%20to%20checkmark%20the%20following%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2261%22%3EAuthorization%20code%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2262%22%3EClient%20credentials%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2264%22%3EFor%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EDefault%20scope%3C%2FEM%3E%2C%20you%20can%20either%20leave%20it%20blank%20or%20enter%20any%20valid%20scope%20as%20defined%20in%20MITREid%20Connect%20client%20registration.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2266%22%3EYou%20can%20keep%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EResource%20owner%20username%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EResource%20owner%20password%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eas%20blank.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2268%22%3EFor%20additional%20details%20on%20creating%20OAuth2%20server%20in%20APIM%2C%20please%20see%20this%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fapi-management-howto-protect-backend-with-aad%23enable-oauth-20-user-authorization-in-the-developer-console%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fapi-management-howto-protect-backend-with-aad%23enable-oauth-20-user-authorization-in-the-developer-console%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fapi-management-howto-protect-backend-with-aad%23enable-oauth-20-user-authorization-in-the-developer-console%22%3Edocument%3C%2FA%3E.%3C%2FP%3E%0A%3CH2%20class%3D%22code-line%22%20data-line%3D%2270%22%20id%3D%22toc-hId-1477396891%22%20id%3D%22toc-hId-1477396890%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22set-up-a-rest-api-app-in-apim%22%20class%3D%22code-line%22%20data-line%3D%2270%22%20id%3D%22toc-hId--330057572%22%20id%3D%22toc-hId--330057573%22%3ESet%20up%20a%20REST%20API%20App%20in%20APIM%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2272%22%3ECreate%20an%20API%20Management%20service%20by%20following%20this%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fget-started-create-service-instance%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fget-started-create-service-instance%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fget-started-create-service-instance%22%3Edocument%3C%2FA%3E.%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2273%22%3EImport%20a%20REST%20API%20by%20following%20this%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fimport-and-publish%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fimport-and-publish%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapi-management%2Fimport-and-publish%22%3Edocument%3C%2FA%3E.%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2274%22%3EMake%20sure%20to%20disable%20subscription-key%20authorization%20which%20is%20enabled%20by%20default.%3COL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2275%22%3EClick%20on%20%22APIs%22%20menu%20in%20the%20left%20menu%20column%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2276%22%3ESelect%20the%20API%20you%20are%20working%20with%20in%20the%20mid-column%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2277%22%3EIn%20the%20right%20pane%2C%20select%20Settings%20tab%20at%20the%20top%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%2278%22%3EUncheck%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3ESubscription%20required%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eunder%20Subscription%20section.%20This%20ensures%20that%20subscription-key%20authorization%20is%20not%20used%20since%20we%20intend%20to%20use%20OAuth2%20authorization.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH2%20class%3D%22code-line%22%20data-line%3D%2280%22%20id%3D%22toc-hId--463742296%22%20id%3D%22toc-hId--463742297%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22customize-api-inbound-policy%22%20class%3D%22code-line%22%20data-line%3D%2280%22%20id%3D%22toc-hId-2023770537%22%20id%3D%22toc-hId-2023770536%22%3ECustomize%20API%20Inbound%20Policy%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22code-line%22%20data-line%3D%2282%22%3E%3CCODE%3E%20%20%3CVALIDATE-JWT%20header-name%3D%22Authorization%22%20failed-validation-httpcode%3D%22401%22%20failed-validation-error-message%3D%22Unauthorized.%20Access%20token%20is%20missing%20or%20invalid.%22%3E%0A%20%20%20%20%20%20%20%20%3COPENID-CONFIG%20url%3D%22%5Bopenid-config%20endpoint%5D%22%3E%3C%2FOPENID-CONFIG%3E%0A%20%20%20%20%20%20%20%20%3CREQUIRED-CLAIMS%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CCLAIM%20name%3D%22aud%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CVALUE%3E%5Baudience%5D%3C%2FVALUE%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FCLAIM%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CCLAIM%20name%3D%22iss%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CVALUE%3E%5Bissuer%5D%3CVALUE%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FVALUE%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CCLAIM%20name%3D%22scope%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CVALUE%3E%5Bscope%201%5D%3C%2FVALUE%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CVALUE%3E%5Bscope%202%5D%3C%2FVALUE%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FCLAIM%3E%0A%20%20%20%20%20%20%20%20%3C%2FVALUE%3E%0A%20%20%3C%2FCLAIM%3E%0A%3C%2FREQUIRED-CLAIMS%3E%3C%2FVALIDATE-JWT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%2298%22%3EThe%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3Eopenid-config%20endpoint%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eis%20the%20OpenID%20Config%20URL%20from%20MITREid%20Connect.%20It%20is%20critical%20to%20make%20sure%20you%20have%20the%20correct%20URL%20since%20this%20is%20the%20URL%20thru%20which%20APIM%20acquires%20JWKS%20(JSON%20Web%20Key%20Set)%20for%20JWT%20validation.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22100%22%3EThe%20values%20for%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3Eaudience%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20*issuer%22%20are%20from%20MITREid%20Connect%20and%20can%20be%20found%20from%20its%20token%20sample.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22102%22%3EFor%20*scope%22%20claim%2C%20you%20can%20have%200%2C%201%20or%20multiple%20scope%20values.%20Notice%20that%20if%20you%20have%20mutlipel%20scope%20values%2C%20it%20means%20AND%20(all%20required)%20instead%20of%20OR%20(one%20of%20scope%20values%20is%20enough).%3C%2FP%3E%0A%3CH2%20class%3D%22code-line%22%20data-line%3D%22104%22%20id%3D%22toc-hId-216316074%22%20id%3D%22toc-hId-216316073%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22troubleshooting-guide%22%20class%3D%22code-line%22%20data-line%3D%22104%22%20id%3D%22toc-hId--1591138389%22%20id%3D%22toc-hId--1591138390%22%3ETroubleshooting%20Guide%3C%2FH2%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22106%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22106%22%3EIn%20OAuth2%20authorization%2C%20it%20is%20typical%20to%20have%20multiple%20parts%20involved%20in%20the%20authorization%20flow%3A%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22106%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3EComponent%3C%2FTH%3E%0A%3CTH%3EPurpose%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3EClient%20application%3C%2FTD%3E%0A%3CTD%3EThe%20client%20application%20can%20be%20either%20an%20app%20for%20end%20users%20or%20a%20service%2Fserver%20process%20depending%20on%20the%20secured%20REST%20API.%20In%20case%20of%20a%20client%20app%20for%20end%20users%2C%20it%20can%20be%20either%20a%20private%20client%20which%20can%20hide%20client_secret%20or%20a%20public%20client%20which%20cannot.%20Depending%20on%20use%20case%2C%20different%20OAuth2%20authorization%20flows%20can%20be%20used.%20If%20the%20client%20is%20a%20service%2C%20a%20Client%20Credentials%20Flow%20is%20used.%20For%20a%20public%20end%20user%20client%2C%20Authorization%20Code%20Flow%20with%20PKCE%20is%20used%20such%20as%20in%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22http%3A%2F%2Faka.ms%2Fott%22%20href%3D%22http%3A%2F%2Faka.ms%2Fott%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22http%3A%2F%2Faka.ms%2Fott%22%3EOAuth2%20Test%20Tool%3C%2FA%3E.%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EIdentity%20Provider%3C%2FTD%3E%0A%3CTD%3EUsers%20get%20authenticated%20and%20are%20issued%20authorization%20code%20which%20can%20be%20used%20to%20acquire%20access_token.%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EToken%20issuer%3C%2FTD%3E%0A%3CTD%3EThe%20MITREid%20Connect%20component%20which%20issues%20access_tokens%20and%20refresh_tokens%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EToken%20introspector%3C%2FTD%3E%0A%3CTD%3EThe%20APIM%20component%20which%20inspects%20and%20validates%20JWT%20tokens%20based%20on%20policy%20settings%2C%20such%20as%20claims%2C%20issuer%20and%20audience%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3EOpenID%20config%20endpoint%3C%2FTD%3E%0A%3CTD%3EAn%20endpoint%20provided%20by%20MITREid%20Connect%20from%20which%20APIM%20acquires%20public%20keys%20for%20token%20introspection.%20This%20is%20based%20on%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22https%3A%2F%2Fopenid.net%2Fspecs%2Fopenid-connect-discovery-1_0.html%22%20href%3D%22https%3A%2F%2Fopenid.net%2Fspecs%2Fopenid-connect-discovery-1_0.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Fopenid.net%2Fspecs%2Fopenid-connect-discovery-1_0.html%22%3EOpenID%20Discovery%20spec%3C%2FA%3E.%20APIM%20is%20never%20configured%20to%20hold%20a%20static%20public%20key%20from%20MITREid%20Connect.%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3ERegistered%20client%20app%3C%2FTD%3E%0A%3CTD%3EA%20client%20app%20pointer%20in%20MITREid%20Connect%20which%20defines%20the%20scopes%20granted%20by%20either%20admin%20or%20users%2C%20as%20well%20as%20its%20client%20ID%20and%20client_secret%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3ERegistered%20server%20app%3C%2FTD%3E%0A%3CTD%3EA%20server%20app%20pointer%20in%20MITREid%20Connect%20which%20defines%20an%20abstraction%20of%20REST%20API%20and%20defines%20the%20scopes%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CH3%20class%3D%22code-line%22%20data-line%3D%22119%22%20id%3D%22toc-hId-1025457163%22%20id%3D%22toc-hId-1025457162%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22unauthorized-401-issue%22%20class%3D%22code-line%22%20data-line%3D%22119%22%20id%3D%22toc-hId--781997300%22%20id%3D%22toc-hId--781997301%22%3EUnauthorized%20(401)%20Issue%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22121%22%3EThe%20most%20common%20error%20in%20OAuth2%20authorization%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EUnauthorized%20(401)%3C%2FEM%3E.%20This%20could%20be%20caused%20by%20any%20of%20the%20following%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22123%22%3EIncorrect%20openid-config%20endpoint%20in%20API%20inbound%20policy%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22124%22%3EIncorrect%20*iss%22%20value%20in%20API%20inbound%20policy%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22125%22%3EIncorrect%20%22aud%22%20value%20in%20API%20inbound%20policy%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22126%22%3EIncorrect%20%22scope%22%20values%20in%20API%20inbound%20policy.%20If%20you%20can%20capture%20a%20sample%20JWT%20token%2C%20you%20can%20parse%20it%20via%20a%20tool%20like%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22http%3A%2F%2Faka.ms%2Fjwt%22%20href%3D%22http%3A%2F%2Faka.ms%2Fjwt%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22http%3A%2F%2Faka.ms%2Fjwt%22%3Ehttp%3A%2F%2Faka.ms%2Fjwt%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eto%20see%20its%20claims%20values.%20These%20values%20are%20determined%20by%20registered%20server%20application%20in%20MITREid%20Connect%20and%20the%20API%20permission%20configurations%20for%20the%20registered%20client%20application.%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22127%22%3EIncorrect%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3ESecurity%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Esettings%20in%20the%20API.%20Check%20the%20*Settings%22%20tab%20of%20the%20API%20and%20make%20sure%20that%20*User%20authorization%22%20is%20OAuth%202.0%20and%20the%20right%20OAuth%202.0%20server%20is%20selected%20in%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EOAuth%202.0%20server%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Edropdown.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH3%20class%3D%22code-line%22%20data-line%3D%22129%22%20id%3D%22toc-hId-1705515533%22%20id%3D%22toc-hId-1705515532%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22subscriptio-key-issue%22%20class%3D%22code-line%22%20data-line%3D%22129%22%20id%3D%22toc-hId--101938930%22%20id%3D%22toc-hId--101938931%22%3ESubscription-key%20Issue%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22131%22%3EBy%20default%2C%20when%20an%20API%20is%20installed%20into%20APIM%20service%2C%20its%20subscription-key%20authorization%20is%20enabled.%20Make%20sure%20it%20is%20disabled%20since%20we%20use%20OAuth2%20instead%20of%20subscription-key.%20You%20can%20find%20its%20settings%20under%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3ESettings%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Etab%20in%20the%20API.%3C%2FP%3E%0A%3CH3%20class%3D%22code-line%22%20data-line%3D%22133%22%20id%3D%22toc-hId--1909393393%22%20id%3D%22toc-hId--1909393394%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22cors-issue%22%20class%3D%22code-line%22%20data-line%3D%22133%22%20id%3D%22toc-hId-578119440%22%20id%3D%22toc-hId-578119439%22%3ECORS%20Issue%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22135%22%3EIf%20your%20test%20client%20is%20a%20web%20or%20SPA%20client%20and%20you%20use%20javascript%20to%20make%20API%20calls%20with%20OAuth2%20authorization%2C%20you%20will%20likely%20face%20CORS%20issue%20since%20the%20web%20app%20or%20SPA%20is%20from%20different%20domain%20as%20APIM.%20By%20default%2C%20an%20API%20in%20APIM%20does%20not%20support%20CORS%20preflight%20and%20you%20need%20to%20enable%20it%20in%20inbound%20policy.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22137%22%3EInbound%20section%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22code-line%22%20data-line%3D%22139%22%3E%3CCODE%3E%20%20%3CCORS%20allow-credentials%3D%22true%22%3E%0A%20%20%20%20%20%20%20%20%3CALLOWED-ORIGINS%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CORIGIN%3Ehttp%3A%2F%2Flocalhost%3A3000%3C%2FORIGIN%3E%0A%20%20%20%20%20%20%20%20%3C%2FALLOWED-ORIGINS%3E%0A%20%20%20%20%20%20%20%20%3CALLOWED-METHODS%20preflight-result-max-age%3D%22300%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CMETHOD%3EGET%3C%2FMETHOD%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CMETHOD%3EPOST%3C%2FMETHOD%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CMETHOD%3EPATCH%3C%2FMETHOD%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CMETHOD%3EDELETE%3C%2FMETHOD%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CMETHOD%3EPUT%3C%2FMETHOD%3E%0A%20%20%20%20%20%20%20%20%3C%2FALLOWED-METHODS%3E%0A%20%20%3C%2FCORS%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22152%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22152%22%3EOutbound%20section%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22code-line%22%20data-line%3D%22154%22%3E%3CCODE%3E%20%20%3COUTBOUND%3E%0A%20%20%20%20%20%3CBASE%20%2F%3E%0A%20%20%20%20%20%3CSET-HEADER%20name%3D%22Access-Control-Allow-Origin%22%20exists-action%3D%22override%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%3CVALUE%3E%40(context.Request.Headers.GetValueOrDefault(%22Origin%22%2C%22%22))%3C%2FVALUE%3E%0A%20%20%20%20%20%3C%2FSET-HEADER%3E%0A%20%20%20%20%20%3CSET-HEADER%20name%3D%22Access-Control-Allow-Credentials%22%20exists-action%3D%22override%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%3CVALUE%3Etrue%3C%2FVALUE%3E%0A%20%20%20%20%20%3C%2FSET-HEADER%3E%0A%20%20%3C%2FOUTBOUND%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH3%20class%3D%22code-line%22%20data-line%3D%22164%22%20id%3D%22toc-hId--531173082%22%20id%3D%22toc-hId--531173083%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22custom-header-issue%22%20class%3D%22code-line%22%20data-line%3D%22164%22%20id%3D%22toc-hId-1956339751%22%20id%3D%22toc-hId-1956339750%22%3ECustom%20header%20issue%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22166%22%3EAnother%20potential%20issue%20is%3A%20for%20a%20production%20REST%20API%2C%20it%20has%20custom%20headers%20such%20as%20transaction%20or%20correlation%20ID%20for%20logging%20and%20troubleshooting.%20Such%20correlation%20ID%20may%20be%20scoped%20to%20the%20enterprise%20or%20the%20service%20itself.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22168%22%3EHowever%2C%20there%20is%20a%20restriction%20to%20access%20response%20headers%20when%20you%20are%20using%20javascript%20Fetch%20API%20over%20CORS.%20Due%20to%20this%20restriction%2C%20by%20default%20you%20can%20access%20only%20following%20standard%20headers%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22169%22%3ECache-Control%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22170%22%3EContent-Language%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22171%22%3EContent-Type%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22172%22%3EExpires%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22173%22%3ELast-Modified%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22174%22%3EPragma%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22176%22%3EMore%20info%20on%20this%20restriction%20can%20be%20found%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22https%3A%2F%2Fdevelopers.google.com%2Fweb%2Fupdates%2F2015%2F03%2Fintroduction-to-fetch%23response_types%22%20href%3D%22https%3A%2F%2Fdevelopers.google.com%2Fweb%2Fupdates%2F2015%2F03%2Fintroduction-to-fetch%23response_types%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Fdevelopers.google.com%2Fweb%2Fupdates%2F2015%2F03%2Fintroduction-to-fetch%23response_types%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22178%22%3EIn%20order%20to%20allow%20such%20client%20to%20get%20access%20to%20custom%20headers%20(such%20as%20correlation%20ID)%2C%20we%20need%20to%20add%20the%20following%20section%20in%20our%20inbound%2Fcors%20section%3A%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22178%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22code-line%22%20data-line%3D%22180%22%3E%3CCODE%3E%20%20%3CALLOWED-HEADERS%3E%0A%20%20%20%20%20%20%20%20%3CHEADER%3Econtent-type%3C%2FHEADER%3E%0A%20%20%20%20%20%20%20%20%3CHEADER%3Eaccept%3C%2FHEADER%3E%0A%20%20%20%20%20%20%20%20%3CHEADER%3Eauthorization%3C%2FHEADER%3E%0A%20%20%20%20%20%20%20%20%3CHEADER%3Ex-correlation-id%3C%2FHEADER%3E%0A%20%20%20%20%20%20%20%20%3CHEADER%3Ex-my-request-id%3C%2FHEADER%3E%0A%20%20%3C%2FALLOWED-HEADERS%3E%0A%20%20%3CEXPOSE-HEADERS%3E%0A%20%20%20%20%20%20%20%20%3CHEADER%3Ex-correlation-id%3C%2FHEADER%3E%0A%20%20%20%20%20%20%20%20%3CHEADER%3Ex-my-request-id%3C%2FHEADER%3E%0A%20%20%3C%2FEXPOSE-HEADERS%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH2%20class%3D%22code-line%22%20data-line%3D%22193%22%20id%3D%22toc-hId-19802569%22%20id%3D%22toc-hId-19802568%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22terraform-for-deployment%22%20class%3D%22code-line%22%20data-line%3D%22193%22%20id%3D%22toc-hId--1787651894%22%20id%3D%22toc-hId--1787651895%22%3ETerraform%20for%20Deployment%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22195%22%3ENeedless%20to%20say%2C%20it%20is%20desirable%20to%20have%20the%20creation%20and%20configuration%20automated.%20We%20could%20use%20Terraform%20for%20this%20purpose.%20Detailed%20document%20on%20Terraform%20Azuure%20API%20Management%20Resources%20can%20be%20found%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20title%3D%22https%3A%2F%2Fwww.terraform.io%2Fdocs%2Fproviders%2Fazurerm%2Fr%2Fapi_management.html%22%20href%3D%22https%3A%2F%2Fwww.terraform.io%2Fdocs%2Fproviders%2Fazurerm%2Fr%2Fapi_management.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%20data-href%3D%22https%3A%2F%2Fwww.terraform.io%2Fdocs%2Fproviders%2Fazurerm%2Fr%2Fapi_management.html%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22197%22%3EThe%20Terraform%20(.tf%20file)%20should%20cover%20the%20following%20tasks%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22199%22%3ECreate%20a%20resource%20group%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22200%22%3ECreate%20an%20API%20Management%20service%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22201%22%3ECreate%20a%20product%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22202%22%3EAdd%20a%20REST%20API%20into%20the%20product%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22203%22%3EDisable%20subscriptin%20key%20authorization%20in%20the%20REST%20API%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22204%22%3EImport%20a%20prepared%20inbound%20policy%20(XML)%20into%20the%20REST%20API%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22205%22%3EAdd%20OAuth2%20Server%20based%20on%20MITREid%20Connect%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22206%22%3EConfigure%20REST%20API%20settings%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22210%22%20data-unlink%3D%22true%22%3EIn%20addition%20to%20the%20variables%20defined%20in%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Evariables.tf%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efile%2C%20the%20Terraform%20also%20requires%20a%20XML%20policy%20file%20as%20input.%20In%20the%20policy%20file%2C%20the%20following%20parameters%20(XML%20node%2Fattribute%20values)%20are%20required.%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22210%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3EXML%20node%3C%2FTH%3E%0A%3CTH%3ENode%20attribute%3C%2FTH%3E%0A%3CTH%3EDefinition%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3Eopenid-config%3C%2FTD%3E%0A%3CTD%3Eurl%3C%2FTD%3E%0A%3CTD%3EThe%20OpenID%20Config%20endpoint%20URL%20for%20public%20keys%20used%20in%20token%20introspection%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3Eclaim%3C%2FTD%3E%0A%3CTD%3Ename%20(aud)%3C%2FTD%3E%0A%3CTD%3EAudience%20defined%20in%20the%20registered%20server%20app%20in%20MITREid%20Connect%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3Eclaim%3C%2FTD%3E%0A%3CTD%3Ename%20(iss)%3C%2FTD%3E%0A%3CTD%3EThe%20issuer%20of%20JWT%20tokens%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3Eclaim%3C%2FTD%3E%0A%3CTD%3Ename%20(scope)%3C%2FTD%3E%0A%3CTD%3EThe%20list%20of%20required%20scopes.%20All%20of%20the%20listed%20scopes%20listed%20must%20be%20present%20in%20a%20token%20before%20it%20can%20be%20validated.%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3Eallowed-origins%3C%2FTD%3E%0A%3CTD%3E%26nbsp%3B%3C%2FTD%3E%0A%3CTD%3EList%20of%20allowed%20origins%20for%20CORS%20preflights%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22221%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22code-line%22%20data-line%3D%22221%22%3EThe%20APIM%20inbound%20policy%20with%20these%20parameters%20is%20critical%20in%20ensuring%20the%20following%20to%20work%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22222%22%3EToken%20introspection%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22223%22%3ECORS%20policy%3C%2FLI%3E%0A%3CLI%20class%3D%22code-line%22%20data-line%3D%22224%22%3ECustom%20header%20accessibility%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1717430%22%20slang%3D%22en-US%22%3E%3CP%3EWhile%20using%20Azure%20AD%20for%20OAuth2%20authorization%20in%20API%20Management%20is%20well%20documented%2C%20what%20if%20a%20customer%20uses%20MITREid%20Connect%20as%20their%20Identity%20Provider%3F%20MITREid%20Connect%20is%20an%20open%20source%20Identity%20Provider%20which%20is%20OAuth2%2FOpenID%20Connect-compliant.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1717430%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20API%20management%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Use MITREid Connect for OAuth2 Authorization in API Management

 

By (alphabetically): Akinlolu Akindele, Dan Balma, Maarten Van De Bospoort, Erin Corson, Nick Drouin, Heba Elayoty, Andrei Ermilov, David Giard, Michael Green, Alfredo Chavez Hernandez, Hao Luo, Maggie Marxen, Siva Mullapudi, Nsikan Udoyen, William Zhang

 

Introduction

 

Using an API gateway in front of REST APIs is a common design pattern which allows us to offload the cross-cutting capabilities such as OAuth2 authorization to the gateway instead of letting security code get scattered in application code. Azure API Management (APIM) is such an API gateway service. For the common case that the OAuth2 server in APIM is Azure AD (AAD), it has been well documented. In this document we will cover the case that the OAuth2 server is MITREid Connect instead of AAD.

 

The following diagram illustrates such a scenario: the backend REST API runs on an Azure Kubernetes Service (AKS) cluster. While the API Management endpoint is secured by OAuth2 authorization, the AKS endpoint is secured by mutual TLS (mTLS) between APIM and AKS. The setup of mTLS between AKS and APIM is covered by a sister document.
 
security_oauth2.drawio.png

 

MITREid Connect is an open source Identity Provider, popular in Java community. MITREid Connect is compliant to OpenID Connect and OAuth 2.0 protocol. This document is based on MITREid Connect v 1.3.3.

 

Prerequisites

 

  1. An Azure subscription
  2. A MITREid Connect instance. This document focuses on how to enable OAuth2 over APIM and MITREid Connect, instead of on how to set up MITREid Connect.
  3. A REST API app for test. The REST API could be deployed to AKS, App Service, or other. Or use this sample REST API with Swagger http://conferenceapi.azurewebsites.net/?format=json.
  4. Familiar with Azure API Management
  5. Basic understanding of OAuth2. OAuth2 involves a few specs (OAuth2, OIDC, PKCE and JOSE). A list of good references on OAuth2 and related specs can be found here.

NOTE: This article does not cover the security between APIM and the REST API which is a separate topic. If a REST API is deployed to AKS, there are a few options which have been documented in Use Azure API Management with microservices deployed in Azure Kubernetes Service.

If APIM Premium is used, we can leverage VNET so that the security between APIM and AKS is simpler. Otherwise, we need to secure the REST calls from APIM to AKS thru technique such as mutual TLS (mTLS) (note that it is mutual TLS, not just TLS). Please see this sister document

 

Preparations in MITREid Connect

 

In order to prepare for OAuth2 setup in APIM, we need to perform the following steps in MITREid Connect:

  1. Register a server application registration;
  2. Define its scopes;
  3. Register a client application;
  4. Specify its permissions: either Delegated Permissions or Application Permissions;
  5. Create a client secret.

The concepts and the steps for the above OAuth2 steps between MITREid Connect and Azure AD are not fundamentally different. You may get detailed steps from Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

 

The following parameters from MITREid Connect must be prepared and available for our setup in APIM.

 

Parameter Typical Format
Client ID A string. Unlike Azure AD, a client ID in MITREid Connect does not have to be a GUID. But it needs to be unique within its tenant.
Client secret Hexadecimal representation of a cryptographically secure pseudorandom number
OpenID config endpoint https://[host]/.well-known/openid-configuration
Authorization endpoint https://[host]/authorize/
Token endpoint https://[host]/token/
Audience A string
Issuer https://[host]/
Default Scopes Optional, and it could have multiple values

 

Create an OAuth2 Server on MITREid Connect

 

  1. In Azure portal left menu column, click "OAuth 2.0"
  2. Click "Add" button on the right pane
  3. You need the following parameters to create the OAuth 2 server on MITREid Connect:
    • Client ID
    • Client secret
    • Authorization endpoint
    • Token endpoint

For Authorization grant types, you may choose to checkmark the following:

  1. Authorization code
  2. Client credentials

For Default scope, you can either leave it blank or enter any valid scope as defined in MITREid Connect client registration.

You can keep Resource owner username and Resource owner password as blank.

For additional details on creating OAuth2 server in APIM, please see this document.

 

Set up a REST API App in APIM

 

  1. Create an API Management service by following this document.
  2. Import a REST API by following this document.
  3. Make sure to disable subscription-key authorization which is enabled by default.
    1. Click on "APIs" menu in the left menu column
    2. Select the API you are working with in the mid-column
    3. In the right pane, select Settings tab at the top
    4. Uncheck Subscription required under Subscription section. This ensures that subscription-key authorization is not used since we intend to use OAuth2 authorization.

 

Customize API Inbound Policy

 

  <validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
        <openid-config url="[openid-config endpoint]" />
        <required-claims>
              <claim name="aud">
                 <value>[audience]</value>
              </claim>
              <claim name="iss">
              <value>[issuer]<value>
              </claim>
              <claim name="scope">
                 <value>[scope 1]</value>
                 <value>[scope 2]</value>
              </claim>
        </required-claims>
  </validate-jwt>

The openid-config endpoint is the OpenID Config URL from MITREid Connect. It is critical to make sure you have the correct URL since this is the URL thru which APIM acquires JWKS (JSON Web Key Set) for JWT validation.

The values for audience and *issuer" are from MITREid Connect and can be found from its token sample.

For *scope" claim, you can have 0, 1 or multiple scope values. Notice that if you have mutlipel scope values, it means AND (all required) instead of OR (one of scope values is enough).

 

Troubleshooting Guide

 

In OAuth2 authorization, it is typical to have multiple parts involved in the authorization flow:

 

Component Purpose
Client application The client application can be either an app for end users or a service/server process depending on the secured REST API. In case of a client app for end users, it can be either a private client which can hide client_secret or a public client which cannot. Depending on use case, different OAuth2 authorization flows can be used. If the client is a service, a Client Credentials Flow is used. For a public end user client, Authorization Code Flow with PKCE is used such as in the OAuth2 Test Tool.
Identity Provider Users get authenticated and are issued authorization code which can be used to acquire access_token.
Token issuer The MITREid Connect component which issues access_tokens and refresh_tokens
Token introspector The APIM component which inspects and validates JWT tokens based on policy settings, such as claims, issuer and audience
OpenID config endpoint An endpoint provided by MITREid Connect from which APIM acquires public keys for token introspection. This is based on OpenID Discovery spec. APIM is never configured to hold a static public key from MITREid Connect.
Registered client app A client app pointer in MITREid Connect which defines the scopes granted by either admin or users, as well as its client ID and client_secret
Registered server app A server app pointer in MITREid Connect which defines an abstraction of REST API and defines the scopes

 

Unauthorized (401) Issue

 

The most common error in OAuth2 authorization is Unauthorized (401). This could be caused by any of the following:

  1. Incorrect openid-config endpoint in API inbound policy
  2. Incorrect *iss" value in API inbound policy
  3. Incorrect "aud" value in API inbound policy
  4. Incorrect "scope" values in API inbound policy. If you can capture a sample JWT token, you can parse it via a tool like http://aka.ms/jwt to see its claims values. These values are determined by registered server application in MITREid Connect and the API permission configurations for the registered client application.
  5. Incorrect Security settings in the API. Check the *Settings" tab of the API and make sure that *User authorization" is OAuth 2.0 and the right OAuth 2.0 server is selected in the OAuth 2.0 server dropdown.

 

Subscription-key Issue

 

By default, when an API is installed into APIM service, its subscription-key authorization is enabled. Make sure it is disabled since we use OAuth2 instead of subscription-key. You can find its settings under Settings tab in the API.

 

CORS Issue

 

If your test client is a web or SPA client and you use javascript to make API calls with OAuth2 authorization, you will likely face CORS issue since the web app or SPA is from different domain as APIM. By default, an API in APIM does not support CORS preflight and you need to enable it in inbound policy.

Inbound section:

  <cors allow-credentials="true">
        <allowed-origins>
            <origin>http://localhost:3000</origin>
        </allowed-origins>
        <allowed-methods preflight-result-max-age="300">
            <method>GET</method>
            <method>POST</method>
            <method>PATCH</method>
            <method>DELETE</method>
            <method>PUT</method>
        </allowed-methods>
  </cors>

 

Outbound section:

  <outbound>
     <base />
     <set-header name="Access-Control-Allow-Origin" exists-action="override">
           <value>@(context.Request.Headers.GetValueOrDefault("Origin",""))</value>
     </set-header>
     <set-header name="Access-Control-Allow-Credentials" exists-action="override">
           <value>true</value>
     </set-header>
  </outbound>

 

Custom header issue

 

Another potential issue is: for a production REST API, it has custom headers such as transaction or correlation ID for logging and troubleshooting. Such correlation ID may be scoped to the enterprise or the service itself.

However, there is a restriction to access response headers when you are using javascript Fetch API over CORS. Due to this restriction, by default you can access only following standard headers:

  • Cache-Control
  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma

More info on this restriction can be found here.

In order to allow such client to get access to custom headers (such as correlation ID), we need to add the following section in our inbound/cors section:

 

  <allowed-headers>
        <header>content-type</header>
        <header>accept</header>
        <header>authorization</header>
        <header>x-correlation-id</header>
        <header>x-my-request-id</header>
  </allowed-headers>
  <expose-headers>
        <header>x-correlation-id</header>
        <header>x-my-request-id</header>
  </expose-headers>

 

Terraform for Deployment

 

Needless to say, it is desirable to have the creation and configuration automated. We could use Terraform for this purpose. Detailed document on Terraform Azuure API Management Resources can be found here.

The Terraform (.tf file) should cover the following tasks:

  1. Create a resource group
  2. Create an API Management service
  3. Create a product
  4. Add a REST API into the product
  5. Disable subscriptin key authorization in the REST API
  6. Import a prepared inbound policy (XML) into the REST API
  7. Add OAuth2 Server based on MITREid Connect
  8. Configure REST API settings

In addition to the variables defined in variables.tf file, the Terraform also requires a XML policy file as input. In the policy file, the following parameters (XML node/attribute values) are required.

 

XML node Node attribute Definition
openid-config url The OpenID Config endpoint URL for public keys used in token introspection
claim name (aud) Audience defined in the registered server app in MITREid Connect
claim name (iss) The issuer of JWT tokens
claim name (scope) The list of required scopes. All of the listed scopes listed must be present in a token before it can be validated.
allowed-origins   List of allowed origins for CORS preflights

 

The APIM inbound policy with these parameters is critical in ensuring the following to work:

  • Token introspection
  • CORS policy
  • Custom header accessibility