Skip to content
This repository was archived by the owner on Feb 2, 2022. It is now read-only.

Commit aa3acaa

Browse files
authored
Update default scope string. #133 #131 (#134)
1 parent 210fed3 commit aa3acaa

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

cli/raft-tools/auth/node-js/msal/msal_token.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
var msal = require('@azure/msal-node');
33

4-
function get_token(client_id, tenant_id, secret, scopes, authority_uri, callback) {
4+
function get_token(client_id, tenant_id, secret, scopes, authority_uri, callback, audience) {
55
let authority;
66
if (authority_uri) {
77
authority = authority_uri + '/' + tenant_id;
@@ -11,7 +11,12 @@ function get_token(client_id, tenant_id, secret, scopes, authority_uri, callback
1111
}
1212

1313
if (!scopes) {
14-
scopes = [client_id + "/.default"];
14+
if (!audience) {
15+
scopes = [client_id + "/.default"];
16+
}
17+
else {
18+
scopes = [audience + "/.default"];
19+
}
1520
}
1621

1722
const msalConfig = {
@@ -48,7 +53,7 @@ exports.tokenFromEnvVariable = function (env_variable_name, callback) {
4853
let auth = JSON.parse(process.env["RAFT_" + env_variable_name] || process.env[env_variable_name]);
4954
if (auth) {
5055
console.log("Getting MSAL token");
51-
get_token(auth['client'], auth['tenant'], auth['secret'], auth['scopes'], auth['authorityUri'], callback);
56+
get_token(auth['client'], auth['tenant'], auth['secret'], auth['scopes'], auth['authorityUri'], callback, auth['audience']);
5257
}
5358
else {
5459
callback(new Error("Authentication parameters are not set in environment variable " + env_variable_name));

cli/raft-tools/auth/python3/msal/msal_token.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@
33
import json
44
import sys
55

6-
def get_token(client_id, tenant_id, secret, scopes, authority_uri):
6+
def get_token(client_id, tenant_id, secret, scopes, authority_uri, audience):
77

88
if authority_uri:
99
authority = f"{authority_uri}/{tenant_id}"
1010
else:
1111
authority = f"https://login.microsoftonline.com/{tenant_id}"
1212

1313
if not scopes:
14-
scopes = [f"{client_id}/.default"]
14+
if not audience:
15+
scopes = [f"{client_id}/.default"]
16+
else:
17+
scopes = [f"{audience}/.default"]
18+
1519
app = msal.ConfidentialClientApplication(client_id, authority=authority, client_credential=secret)
1620
return app.acquire_token_for_client(scopes)
1721

1822
def token_from_env_variable(env_variable_name):
1923
auth_params = os.environ.get(f"RAFT_{env_variable_name}") or os.environ.get(env_variable_name)
2024
if auth_params:
2125
auth = json.loads(auth_params)
22-
token = get_token(auth['client'], auth['tenant'], auth['secret'], auth.get('scopes'), auth.get('authorityUri') )
2326
print("Getting MSAL token")
27+
token = get_token(auth['client'], auth['tenant'], auth['secret'], auth.get('scopes'), auth.get('authorityUri'), auth.get('audience'))
28+
print("Token created")
2429
return f'{token["token_type"]} {token["access_token"]}'
2530
else:
2631
print(f"Authentication parameters are not set in environment variable {env_variable_name}")

docs/how-to-deploy.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ of this object type.
112112
## Step 3: Choose Configuration Options
113113

114114
To deploy RAFT, you will need to settle on a few configuration options in advance.
115-
Note that only four of these are required.
116115

117116
| Option | Required? | Description |
118117
|--------|-------------|--------|

docs/schema/authentication.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ The MSAL configuration JSON blob stored in the key vault should be in this form:
3737
"secret": "<your secret string>"
3838
"scopes": ["example/.default"]
3939
"authorityUri" : "<your authority uri>"
40+
"audience" : "<applicationId>"
4041
}
4142
```
4243
The `client`, `tenant`, and `secret` fields are mandatory.
4344

4445
The optional `scopes` field is an array of strings and has a default value of `["{client}/.default"]`
45-
where `{client}` is the value of the client field in the structure.
46+
where `{client}` is the value of the client field in the structure. If you provide the `audience` field,
47+
the scope will be set to the default value `["{audience}/.default"]`. This is useful when your
48+
application registration service principal is different from the application you are targeting.
49+
50+
**If you provide the `scopes` array with your own values, it will be used as you have defined it, no
51+
defaults will be applied.**
4652

4753
The optional `authorityUri` field is a string and has a default value of
4854
"https://login.microsoftonline.com/{tenant}" where `{tenant}` is the tenant field in the structure.

src/Agent/AzureAuth/Auth.fs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ type AppRegistration =
6969
secret : string
7070
scopes : string array option
7171
authorityUri :string option
72+
audience : string option
7273
}
7374

74-
7575
[<EntryPoint>]
7676
let main argv =
7777
async {
@@ -103,7 +103,11 @@ let main argv =
103103
let auth : AppRegistration = envVar |> loadSecretEnv |> Json.Compact.deserialize
104104
let scopes =
105105
match auth.scopes with
106-
| None -> [|sprintf "%s/.default" auth.client|]
106+
| None ->
107+
// The audience is the applicationId of the service you will be accessing over REST
108+
match auth.audience with
109+
| None -> [|sprintf "%s/.default" auth.client|]
110+
| Some a -> [|sprintf "%s/.default" a|]
107111
| Some s -> s
108112

109113
let cred =

0 commit comments

Comments
 (0)