Skip to content

Commit ab15159

Browse files
BryanFaublecarmmmm
andcommitted
[SYNPY-893] Add multiple profile support (#1194)
* Add multiple profile support --------- Co-authored-by: Carmen Montero <[email protected]> Co-authored-by: BryanFauble <[email protected]>
1 parent 5b36421 commit ab15159

File tree

12 files changed

+922
-334
lines changed

12 files changed

+922
-334
lines changed

docs/tutorials/authentication.md

Lines changed: 109 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Authentication
22

3-
There are multiple ways one can login to Synapse. We recommend users to choose the method that fits their workflow.
3+
There are multiple ways one can login to Synapse. We recommend users choose the method that fits their workflow best.
44

55
## Prerequisites
66

7-
* Create a [Personal Access Token](https://help.synapse.org/docs/Managing-Your-Account.2055405596.html#ManagingYourAccount-PersonalAccessTokens) (**aka: Synapse Auth Token**) token obtained
7+
* Create a [Personal Access Token](https://help.synapse.org/docs/Managing-Your-Account.2055405596.html#ManagingYourAccount-PersonalAccessTokens) (**aka: Synapse Auth Token**) obtained
88
from synapse.org under your Settings.
99
* Note that a token must minimally have the **view** scope to be used with the Synapse Python Client.
1010
* Include **Download** and **Modify** permissions if you are using the Synapse Python Client to follow any subsequent tutorials.
@@ -19,6 +19,7 @@ Use the [synapseclient.login][synapseclient.Synapse.login] function
1919
```python
2020
import synapseclient
2121
syn = synapseclient.login(authToken="authtoken")
22+
#returns Welcome, First Last!
2223
```
2324

2425
### Command Line Client
@@ -37,43 +38,133 @@ Logged in as: username (1234567)
3738

3839
For writing code using the Synapse Python client that is easy to share with others, please do not include your credentials in the code. Instead, please use the `~/.synapseConfig` file to manage your credentials.
3940

41+
The Synapse Python Client supports multiple profiles within the `~/.synapseConfig` file, enabling users to manage credentials for multiple accounts. Each profile is defined in its own `[profile <profile_name>]` section. A default profile can still be defined using `[default]`.
42+
4043
When installing the Synapse Python client, the `~/.synapseConfig` is added to your home directory.
4144

4245
### Automatically modifying the `~/.synapseConfig` file with the Command line Client
4346
You may modify the `~/.synapseConfig` file by utilizing the [command line client command and following the interactive prompts](./command_line_client.md#config):
4447

48+
#### Modifying the synapse config for multiple profiles
49+
4550
<!-- termynal -->
4651
```
4752
> synapse config
48-
Synapse username (Optional):
53+
54+
Synapse username (Optional): $MY_USERNAME
55+
56+
Auth token: $MY_SYNAPSE_TOKEN
57+
58+
Configuration profile name (Optional, 'default' used if not specified): $MY_CONFIG_PROFILE
59+
```
60+
61+
#### Adding or updating a specific profile passed in as a command line argument
62+
<!-- termynal -->
63+
```
64+
> synapse --profile $MY_PROFILE_NAME config
65+
66+
Synapse username (Optional): $MY_USERNAME
4967
5068
Auth token: $MY_SYNAPSE_TOKEN
5169
```
5270

71+
Note: If you encounter a PermissionError
72+
(e.g., `[Errno 13] Permission denied: '/Users/username/.synapseConfig'`), it is likely that the user does not have write permissions to the `~/.synapseConfig` file.
73+
To resolve this, ensure that you have the necessary permissions to modify this file.
74+
You can change the permissions using the following command:
75+
76+
`chmod u+w ~/.synapseConfig`
77+
78+
5379
### Manually modifying the `~/.synapseConfig` file
5480
The following describes how to add your credentials to the `~/.synapseConfig` file without the use of the `synapse config` command.
5581

56-
Open the `~/.synapseConfig` file and find the following section:
82+
Open the `~/.synapseConfig` file using your preferred text editing tool and find/insert the following section(s):
5783

5884
```
85+
[default]
86+
username = default_user
87+
authtoken = default_auth_token
88+
89+
[profile user1]
90+
username = user1
91+
authtoken = user1_auth_token
92+
93+
[profile user2]
94+
username = user2
95+
authtoken = user2_auth_token
96+
97+
# This section is deprecated. It will be used if a `default` profile or a specific profile is not present in the config file
5998
#[authentication]
60-
#username = <username>
61-
#authtoken = <authtoken>
99+
#username = default_user
100+
#authtoken = default_auth_token
62101
```
63102

64-
To enable this section, uncomment it. You don't need to specify your username when using authtoken as a pair, but if you do, it will be used to verify your identity. A personal access token generated from your synapse.org Settings can be used as your .synapseConfig authtoken.
103+
`username` is optional when using `authtoken`, but if provided, an additional check to verify the `authtoken` matches the `username` is performed.
104+
105+
The `authoken` is also know as a personal access token. It is generated from your [synapse.org Settings](https://help.synapse.org/docs/Managing-Your-Account.2055405596.html#ManagingYourAccount-PersonalAccessTokens)..
106+
107+
### Transitioning from One Profile to Multiple
108+
109+
If you're currently using a single profile (under the `[default]` or `[authentication]` section) and wish to start using multiple profiles,
110+
simply add new sections for each profile with a unique profile name. For example, you can add a profile for user1 and user2 as shown below.
111+
The Synapse Python client will allow you to choose which profile to use at login.
112+
65113
```
66-
[authentication]
67-
authtoken = <authtoken>
114+
[default]
115+
username = default_user
116+
authtoken = default_auth_token
117+
118+
[profile user1]
119+
username = user1
120+
authtoken = user1_auth_token
121+
122+
[profile user2]
123+
username = user2
124+
authtoken = user2_auth_token
68125
```
69126

70-
Now, you can login without specifying any arguments:
127+
## Logging in with your ~/.synapseConfig file
128+
129+
**Note:** If no profile is specified the `default` section will be used. Additionally, to support backwards compatibility, `authentication` will continue to function. `authentication` will be used if no profile is used and `default` is not present in the configuration file.
130+
131+
### Logging in via python code
71132

72133
```python
73134
import synapseclient
74135
syn = synapseclient.login()
75136
```
76137

138+
If you want to log in with a specific profile, simply pass the profile name as an argument to `login()`:
139+
140+
```python
141+
import synapseclient
142+
syn = synapseclient.login(profile="user1")
143+
```
144+
145+
### Logging in via the command line
146+
147+
Logs in with the `default` profile, or the profile set in the `SYNAPSE_PROFILE` environment variable:
148+
149+
<!-- termynal -->
150+
```
151+
#For default login
152+
> synapse login
153+
154+
returns Welcome, last first!
155+
```
156+
157+
Logging in with the `profile_name` given:
158+
159+
<!-- termynal -->
160+
```
161+
#For profile login
162+
163+
> synapse --profile profile_name login
164+
165+
Welcome, last first! You are using the 'profile_name' profile.
166+
```
167+
77168
## Use Environment Variable
78169

79170
Setting the `SYNAPSE_AUTH_TOKEN` environment variable will allow you to login to Synapse with a [Personal Access Token](https://help.synapse.org/docs/Managing-Your-Account.2055405596.html#ManagingYourAccount-PersonalAccessTokens)
@@ -86,21 +177,26 @@ In your shell, you can pass an environment variable to Python inline by defining
86177
SYNAPSE_AUTH_TOKEN='<my_personal_access_token>' python3
87178
```
88179

89-
Alternatively you may export it first, then start: Python
180+
Alternatively you may export it first, then start Python:
90181

91182
```bash
92183
export SYNAPSE_AUTH_TOKEN='<my_personal_access_token>'
93184
python3
94185
```
95186

96-
Once you are inside Python, you may simply login without passing any arguments:
187+
Setting the `SYNAPSE_PROFILE` environment variable will allow you to log into Synapse using a specific authentication profile present in your `.synapseConfig` file. This allows you to have multiple profiles present in a single configuration file that you may swap between. Alternatively, you may use the `profile` parameter in Python, or the `--profile` command line flag for all commands like `synapse --profile <profile_name> COMMAND`.
188+
189+
Once you are inside Python, you may simply login without passing any arguments, or pass a profile argument to access a specific profile:
97190

98191
```python
99192
import synapseclient
100193
syn = synapseclient.login()
194+
195+
import synapseclient
196+
syn = synapseclient.login(profile="user1")
101197
```
102198

103-
To use the environment variable with the command line client, simply substitute `python` for the `synapse` command
199+
To use the environment variable with the command line client, simply substitute `python` for the `synapse` command:
104200

105201
```bash
106202
SYNAPSE_AUTH_TOKEN='<my_personal_access_token>' synapse get syn123

0 commit comments

Comments
 (0)