-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for _auth
and email
field in upm credential
#29
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ public class NPMCredential | |
{ | ||
public string url; | ||
public string token; | ||
public string _auth; | ||
public string email; | ||
public bool alwaysAuth; | ||
} | ||
|
||
|
@@ -68,8 +70,14 @@ public CredentialManager() | |
NPMCredential cred = new NPMCredential(); | ||
cred.url = registry.Key; | ||
TomlTable value = (TomlTable)registry.Value; | ||
cred.token = (string)value["token"]; | ||
cred.alwaysAuth = (bool)value["alwaysAuth"]; | ||
if (value.TryGetValue(nameof(NPMCredential.token), out var token)) | ||
cred.token = (string) token; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be much more comfortable with braces around all these conditionals |
||
if (value.TryGetValue(nameof(NPMCredential._auth), out var _auth)) | ||
cred._auth = (string) _auth; | ||
if (value.TryGetValue(nameof(NPMCredential.email), out var email)) | ||
cred.email = (string)email; | ||
if (value.TryGetValue(nameof(NPMCredential.alwaysAuth), out var alwaysAuth)) | ||
cred.alwaysAuth = (bool)alwaysAuth; | ||
|
||
credentials.Add(cred); | ||
} | ||
|
@@ -89,15 +97,17 @@ public void Write() | |
credential.token = ""; | ||
} | ||
|
||
doc.Tables.Add(new TableSyntax(new KeySyntax("npmAuth", credential.url)) | ||
{ | ||
Items = | ||
{ | ||
{"token", credential.token}, | ||
{"alwaysAuth", credential.alwaysAuth} | ||
} | ||
}); | ||
var table = new TableSyntax(new KeySyntax("npmAuth", credential.url)); | ||
|
||
if(!string.IsNullOrEmpty(credential.token)) | ||
table.Items.Add(nameof(NPMCredential.token), credential.token); | ||
if(!string.IsNullOrEmpty(credential._auth)) | ||
table.Items.Add(nameof(NPMCredential._auth), credential._auth); | ||
if(!string.IsNullOrEmpty(credential.email)) | ||
table.Items.Add(nameof(NPMCredential.email), credential.email); | ||
table.Items.Add(nameof(NPMCredential.alwaysAuth), credential.alwaysAuth); | ||
|
||
doc.Tables.Add(table); | ||
} | ||
|
||
|
||
|
@@ -114,21 +124,25 @@ public NPMCredential GetCredential(string url) | |
return credentials.FirstOrDefault(x => x.url?.Equals(url, StringComparison.Ordinal) ?? false); | ||
} | ||
|
||
public void SetCredential(string url, bool alwaysAuth, string token) | ||
public void SetCredential(NPMCredential inputCred) | ||
{ | ||
if (HasRegistry(url)) | ||
if (HasRegistry(inputCred.url)) | ||
{ | ||
var cred = GetCredential(url); | ||
cred.url = url; | ||
cred.alwaysAuth = alwaysAuth; | ||
cred.token = token; | ||
var cred = GetCredential(inputCred.url); | ||
cred.url = inputCred.url; | ||
cred.alwaysAuth = inputCred.alwaysAuth; | ||
cred.token = inputCred.token; | ||
cred.email = inputCred.email; | ||
cred._auth = inputCred._auth; | ||
} | ||
else | ||
{ | ||
NPMCredential newCred = new NPMCredential(); | ||
newCred.url = url; | ||
newCred.alwaysAuth = alwaysAuth; | ||
newCred.token = token; | ||
newCred.url = inputCred.url; | ||
newCred.alwaysAuth = inputCred.alwaysAuth; | ||
newCred.token = inputCred.token; | ||
newCred.email = inputCred.email; | ||
newCred._auth = inputCred._auth; | ||
|
||
credentials.Add(newCred); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,18 @@ public class ScopedRegistry | |
public bool auth; | ||
|
||
public string token; | ||
public string _auth; | ||
public string email; | ||
|
||
public NPMCredential Credential => new NPMCredential() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This creates a new instance each time the property is accessed, it might be better to keep a non-serialized backing field that is initialized when the object is deserialized, or when one of the fields are updated. Although in reality none of the fields should be public, and only have property accessors to begin with, that would actually help when determining when the new credential should be updated. It seems a bit redundant to have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has been so long that I am not sure anymore why I added this one. Probably because I thought it was easier to bundle credentials in a class/struct and pass that instead of each filed separately (like in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really the feedback is more focused on encapsulation. I think providing the property is great, but maybe we might want to properly encapsulate the fields to prevent abusive manipulations of the underlying data. |
||
{ | ||
url = url, | ||
token = token, | ||
_auth = _auth, | ||
email = email, | ||
alwaysAuth = auth | ||
}; | ||
|
||
public override string ToString() | ||
{ | ||
return JsonUtility.ToJson(this, true); | ||
|
@@ -31,7 +42,7 @@ public bool isValidCredential() | |
|
||
if(auth) | ||
{ | ||
if(string.IsNullOrEmpty(token)) | ||
if(string.IsNullOrEmpty(token) && string.IsNullOrEmpty(_auth)) | ||
{ | ||
return false; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any particular reason why this field is formatted differently from the others in this class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the fields use the exact name that is used in the .upmconfig.toml file, which in this case is
_auth
(with the underscore). A nice side effect is that it is possible to usenameof(NPMCredential._auth)
to reference the key.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't aware this was reading/writing from that file. Sounds good to me :) Thanks for helping me understand.