.\" Man page generated from reStructuredText. . .TH "MSALPYTHON" "1" "Jul 05, 2021" "1.9.0" "MSAL Python" .SH NAME msalpython \- MSAL Python Documentation . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .sp You can find high level conceptual documentations in the project \fI\%README\fP and \fI\%workable samples inside the project code base\fP . .sp The documentation hosted here is for API Reference. .sp MSAL proposes a clean separation between \fI\%public client applications and confidential client applications\fP\&. .sp They are implemented as two separated classes, with different methods for different authentication scenarios. .SH PUBLICCLIENTAPPLICATION .INDENT 0.0 .TP .B class msal.PublicClientApplication(client_id, client_credential=None, **kwargs) .INDENT 7.0 .TP .B acquire_token_by_device_flow(flow, claims_challenge=None, **kwargs) Obtain token by a device flow object, with customizable polling effect. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBflow\fP (\fIdict\fP) \-\- A dict previously generated by \fI\%initiate_device_flow()\fP\&. By default, this method\(aqs polling effect will block current thread. You can abort the polling loop at any time, by changing the value of the flow\(aqs "expires_at" key to 0. .IP \(bu 2 \fBclaims_challenge\fP \-\- The claims_challenge parameter requests specific claims requested by the resource provider in the form of a claims_challenge directive in the www\-authenticate header to be returned from the UserInfo Endpoint and/or in the ID Token and/or Access Token. It is a string of a JSON object which contains lists of claims being requested from these locations. .UNINDENT .TP .B Returns A dict representing the json response from AAD: .INDENT 7.0 .IP \(bu 2 A successful response would contain "access_token" key, .IP \(bu 2 an error response would contain "error" and usually "error_description". .UNINDENT .UNINDENT .UNINDENT .INDENT 7.0 .TP .B acquire_token_by_username_password(username, password, scopes, claims_challenge=None, **kwargs) Gets a token for a given resource via user credentials. .sp See this page for constraints of Username Password Flow. \fI\%https://github.com/AzureAD/microsoft\-authentication\-library\-for\-python/wiki/Username\-Password\-Authentication\fP .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBusername\fP (\fIstr\fP) \-\- Typically a UPN in the form of an email address. .IP \(bu 2 \fBpassword\fP (\fIstr\fP) \-\- The password. .IP \(bu 2 \fBscopes\fP (\fIlist\fP\fI[\fP\fIstr\fP\fI]\fP) \-\- Scopes requested to access a protected API (a resource). .IP \(bu 2 \fBclaims_challenge\fP \-\- The claims_challenge parameter requests specific claims requested by the resource provider in the form of a claims_challenge directive in the www\-authenticate header to be returned from the UserInfo Endpoint and/or in the ID Token and/or Access Token. It is a string of a JSON object which contains lists of claims being requested from these locations. .UNINDENT .TP .B Returns A dict representing the json response from AAD: .INDENT 7.0 .IP \(bu 2 A successful response would contain "access_token" key, .IP \(bu 2 an error response would contain "error" and usually "error_description". .UNINDENT .UNINDENT .UNINDENT .INDENT 7.0 .TP .B acquire_token_interactive(scopes, prompt=None, login_hint=None, domain_hint=None, claims_challenge=None, timeout=None, port=None, extra_scopes_to_consent=None, **kwargs) Acquire token interactively i.e. via a local browser. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBscope\fP (\fIlist\fP) \-\- It is a list of case\-sensitive strings. .IP \(bu 2 \fBprompt\fP (\fIstr\fP) \-\- By default, no prompt value will be sent, not even "none". You will have to specify a value explicitly. Its valid values are defined in Open ID Connect specs \fI\%https://openid.net/specs/openid\-connect\-core\-1_0.html#AuthRequest\fP .IP \(bu 2 \fBlogin_hint\fP (\fIstr\fP) \-\- Optional. Identifier of the user. Generally a User Principal Name (UPN). .IP \(bu 2 \fBdomain_hint\fP \-\- .sp Can be one of "consumers" or "organizations" or your tenant domain "contoso.com". If included, it will skip the email\-based discovery process that user goes through on the sign\-in page, leading to a slightly more streamlined user experience. More information on possible values \fI\%here\fP and \fI\%here\fP\&. .IP \(bu 2 \fBclaims_challenge\fP \-\- The claims_challenge parameter requests specific claims requested by the resource provider in the form of a claims_challenge directive in the www\-authenticate header to be returned from the UserInfo Endpoint and/or in the ID Token and/or Access Token. It is a string of a JSON object which contains lists of claims being requested from these locations. .IP \(bu 2 \fBtimeout\fP (\fIint\fP) \-\- This method will block the current thread. This parameter specifies the timeout value in seconds. Default value \fBNone\fP means wait indefinitely. .IP \(bu 2 \fBport\fP (\fIint\fP) \-\- The port to be used to listen to an incoming auth response. By default we will use a system\-allocated port. (The rest of the redirect_uri is hard coded as \fBhttp://localhost\fP\&.) .IP \(bu 2 \fBextra_scopes_to_consent\fP (\fIlist\fP) \-\- "Extra scopes to consent" is a concept only available in AAD. It refers to other resources you might want to prompt to consent for, in the same interaction, but for which you won\(aqt get back a token for in this particular operation. .UNINDENT .TP .B Returns .INDENT 7.0 .IP \(bu 2 A dict containing no "error" key, and typically contains an "access_token" key, if cache lookup succeeded. .IP \(bu 2 A dict containing an "error" key, when token refresh failed. .UNINDENT .UNINDENT .UNINDENT .INDENT 7.0 .TP .B initiate_device_flow(scopes=None, **kwargs) Initiate a Device Flow instance, which will be used in \fI\%acquire_token_by_device_flow()\fP\&. .INDENT 7.0 .TP .B Parameters \fBscopes\fP (\fIlist\fP\fI[\fP\fIstr\fP\fI]\fP) \-\- Scopes requested to access a protected API (a resource). .TP .B Returns A dict representing a newly created Device Flow object. .INDENT 7.0 .IP \(bu 2 A successful response would contain "user_code" key, among others .IP \(bu 2 an error response would contain some other readable key/value pairs. .UNINDENT .UNINDENT .UNINDENT .UNINDENT .SH CONFIDENTIALCLIENTAPPLICATION .INDENT 0.0 .TP .B class msal.ConfidentialClientApplication(client_id, client_credential=None, authority=None, validate_authority=True, token_cache=None, http_client=None, verify=True, proxies=None, timeout=None, client_claims=None, app_name=None, app_version=None, client_capabilities=None) .INDENT 7.0 .TP .B acquire_token_for_client(scopes, claims_challenge=None, **kwargs) Acquires token for the current confidential client, not for an end user. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBscopes\fP (\fIlist\fP\fI[\fP\fIstr\fP\fI]\fP) \-\- (Required) Scopes requested to access a protected API (a resource). .IP \(bu 2 \fBclaims_challenge\fP \-\- The claims_challenge parameter requests specific claims requested by the resource provider in the form of a claims_challenge directive in the www\-authenticate header to be returned from the UserInfo Endpoint and/or in the ID Token and/or Access Token. It is a string of a JSON object which contains lists of claims being requested from these locations. .UNINDENT .TP .B Returns A dict representing the json response from AAD: .INDENT 7.0 .IP \(bu 2 A successful response would contain "access_token" key, .IP \(bu 2 an error response would contain "error" and usually "error_description". .UNINDENT .UNINDENT .UNINDENT .INDENT 7.0 .TP .B acquire_token_on_behalf_of(user_assertion, scopes, claims_challenge=None, **kwargs) Acquires token using on\-behalf\-of (OBO) flow. .sp The current app is a middle\-tier service which was called with a token representing an end user. The current app can use such token (a.k.a. a user assertion) to request another token to access downstream web API, on behalf of that user. See \fI\%detail docs here\fP . .sp The current middle\-tier app has no user interaction to obtain consent. See how to gain consent upfront for your middle\-tier app from this article. \fI\%https://docs.microsoft.com/en\-us/azure/active\-directory/develop/v2\-oauth2\-on\-behalf\-of\-flow#gaining\-consent\-for\-the\-middle\-tier\-application\fP .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBuser_assertion\fP (\fIstr\fP) \-\- The incoming token already received by this app .IP \(bu 2 \fBscopes\fP (\fIlist\fP\fI[\fP\fIstr\fP\fI]\fP) \-\- Scopes required by downstream API (a resource). .IP \(bu 2 \fBclaims_challenge\fP \-\- The claims_challenge parameter requests specific claims requested by the resource provider in the form of a claims_challenge directive in the www\-authenticate header to be returned from the UserInfo Endpoint and/or in the ID Token and/or Access Token. It is a string of a JSON object which contains lists of claims being requested from these locations. .UNINDENT .TP .B Returns A dict representing the json response from AAD: .INDENT 7.0 .IP \(bu 2 A successful response would contain "access_token" key, .IP \(bu 2 an error response would contain "error" and usually "error_description". .UNINDENT .UNINDENT .UNINDENT .UNINDENT .SH SHARED METHODS .sp Both PublicClientApplication and ConfidentialClientApplication have following methods inherited from their base class. You typically do not need to initiate this base class, though. .INDENT 0.0 .TP .B class msal.ClientApplication(client_id, client_credential=None, authority=None, validate_authority=True, token_cache=None, http_client=None, verify=True, proxies=None, timeout=None, client_claims=None, app_name=None, app_version=None, client_capabilities=None) .INDENT 7.0 .TP .B __init__(client_id, client_credential=None, authority=None, validate_authority=True, token_cache=None, http_client=None, verify=True, proxies=None, timeout=None, client_claims=None, app_name=None, app_version=None, client_capabilities=None) Create an instance of application. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBclient_id\fP (\fIstr\fP) \-\- Your app has a client_id after you register it on AAD. .IP \(bu 2 \fBclient_credential\fP (\fIstr\fP) \-\- .sp For \fI\%PublicClientApplication\fP, you simply use \fINone\fP here. For \fI\%ConfidentialClientApplication\fP, it can be a string containing client secret, or an X509 certificate container in this form: .INDENT 2.0 .INDENT 3.5 .sp .nf .ft C { "private_key": "...\-\-\-\-\-BEGIN PRIVATE KEY\-\-\-\-\-...", "thumbprint": "A1B2C3D4E5F6...", "public_certificate": "...\-\-\-\-\-BEGIN CERTIFICATE\-\-\-\-\-... (Optional. See below.)", "passphrase": "Passphrase if the private_key is encrypted (Optional. Added in version 1.6.0)", } .ft P .fi .UNINDENT .UNINDENT .sp \fIAdded in version 0.5.0\fP: public_certificate (optional) is public key certificate which will be sent through \(aqx5c\(aq JWT header only for subject name and issuer authentication to support cert auto rolls. .sp Per \fI\%specs\fP, "the certificate containing the public key corresponding to the key used to digitally sign the JWS MUST be the first certificate. This MAY be followed by additional certificates, with each subsequent certificate being the one used to certify the previous one." However, your certificate\(aqs issuer may use a different order. So, if your attempt ends up with an error AADSTS700027 \- "The provided signature value did not match the expected signature value", you may try use only the leaf cert (in PEM/str format) instead. .IP \(bu 2 \fBclient_claims\fP (\fIdict\fP) \-\- .sp \fIAdded in version 0.5.0\fP: It is a dictionary of extra claims that would be signed by by this \fI\%ConfidentialClientApplication\fP \(aqs private key. For example, you can use {"client_ip": "x.x.x.x"}. You may also override any of the following default claims: .INDENT 2.0 .INDENT 3.5 .sp .nf .ft C { "aud": the_token_endpoint, "iss": self.client_id, "sub": same_as_issuer, "exp": now + 10_min, "iat": now, "jti": a_random_uuid } .ft P .fi .UNINDENT .UNINDENT .IP \(bu 2 \fBauthority\fP (\fIstr\fP) \-\- A URL that identifies a token authority. It should be of the format \fI\%https://login.microsoftonline.com/your_tenant\fP By default, we will use \fI\%https://login.microsoftonline.com/common\fP .IP \(bu 2 \fBvalidate_authority\fP (\fIbool\fP) \-\- (optional) Turns authority validation on or off. This parameter default to true. .IP \(bu 2 \fBcache\fP (\fITokenCache\fP) \-\- Sets the token cache used by this ClientApplication instance. By default, an in\-memory cache will be created and used. .IP \(bu 2 \fBhttp_client\fP \-\- (optional) Your implementation of abstract class HttpClient Defaults to a requests session instance .IP \(bu 2 \fBverify\fP \-\- (optional) It will be passed to the \fI\%verify parameter in the underlying requests library\fP This does not apply if you have chosen to pass your own Http client .IP \(bu 2 \fBproxies\fP \-\- (optional) It will be passed to the \fI\%proxies parameter in the underlying requests library\fP This does not apply if you have chosen to pass your own Http client .IP \(bu 2 \fBtimeout\fP \-\- (optional) It will be passed to the \fI\%timeout parameter in the underlying requests library\fP This does not apply if you have chosen to pass your own Http client .IP \(bu 2 \fBapp_name\fP \-\- (optional) You can provide your application name for Microsoft telemetry purposes. Default value is None, means it will not be passed to Microsoft. .IP \(bu 2 \fBapp_version\fP \-\- (optional) You can provide your application version for Microsoft telemetry purposes. Default value is None, means it will not be passed to Microsoft. .IP \(bu 2 \fBclient_capabilities\fP (\fIlist\fP\fI[\fP\fIstr\fP\fI]\fP) \-\- .sp (optional) Allows configuration of one or more client capabilities, e.g. ["CP1"]. .sp Client capability is meant to inform the Microsoft identity platform (STS) what this client is capable for, so STS can decide to turn on certain features. For example, if client is capable to handle \fIclaims challenge\fP, STS can then issue CAE access tokens to resources knowing when the resource emits \fIclaims challenge\fP the client will be capable to handle. .sp Implementation details: Client capability is implemented using "claims" parameter on the wire, for now. MSAL will combine them into .nf \(gaclaims parameter