@@ -6,6 +6,7 @@ namespace Microsoft.Graph.PowerShell.Authentication.TokenCache
66{
77 using Microsoft . Graph . PowerShell . Authentication . TokenCache . NativePlatformLibs ;
88 using System ;
9+ using System . Globalization ;
910 using System . Runtime . InteropServices ;
1011
1112 /// <summary>
@@ -14,80 +15,112 @@ namespace Microsoft.Graph.PowerShell.Authentication.TokenCache
1415 internal static class LinuxTokenCache
1516 {
1617 /// <summary>
17- /// Gets an app's token from Linux kerings faciility .
18+ /// Gets an app's token from Linux keyrings facility .
1819 /// </summary>
1920 /// <param name="appId">An app/client id.</param>
20- /// <returns>A decypted token.</returns>
21+ /// <returns>A decrypted token.</returns>
2122 public static byte [ ] GetToken ( string appId )
2223 {
24+ if ( string . IsNullOrEmpty ( appId ) )
25+ {
26+ throw new ArgumentNullException ( string . Format (
27+ CultureInfo . CurrentCulture ,
28+ ErrorConstants . Message . NullOrEmptyParameter ,
29+ nameof ( appId ) ) ) ;
30+ }
31+
2332 int key = LinuxNativeKeyUtils . request_key (
2433 type : LinuxNativeKeyUtils . KeyTypes . User ,
25- description : $ "{ Constants . TokenCahceServiceName } :{ appId } ",
34+ description : $ "{ Constants . TokenCacheServiceName } :{ appId } ",
2635 callout_info : IntPtr . Zero ,
2736 dest_keyring : ( int ) LinuxNativeKeyUtils . KeyringType . KEY_SPEC_SESSION_KEYRING ) ;
2837
2938 if ( key == - 1 )
3039 return new byte [ 0 ] ;
3140
32- LinuxNativeKeyUtils . keyctl_read_alloc (
33- key : key ,
34- buffer : out IntPtr contentPtr ) ;
41+ LinuxNativeKeyUtils . keyctl_read_alloc ( key : key , buffer : out IntPtr contentPtr ) ;
3542 string content = Marshal . PtrToStringAnsi ( contentPtr ) ;
3643 Marshal . FreeHGlobal ( contentPtr ) ;
3744
3845 if ( string . IsNullOrEmpty ( content ) )
46+ {
3947 return new byte [ 0 ] ;
48+ }
4049
4150 return Convert . FromBase64String ( content ) ;
4251 }
4352
4453 /// <summary>
45- /// Adds or updates an app's token to Linux kerings faciility .
54+ /// Adds or updates an app's token to Linux keyrings facility .
4655 /// </summary>
4756 /// <param name="appId">An app/client id.</param>
4857 /// <param name="plainContent">The content to store.</param>
4958 public static void SetToken ( string appId , byte [ ] plainContent )
5059 {
51- if ( plainContent != null && plainContent . Length > 0 )
60+ if ( string . IsNullOrEmpty ( appId ) )
5261 {
53- string encodedContent = Convert . ToBase64String ( plainContent ) ;
54- int key = LinuxNativeKeyUtils . request_key (
55- type : LinuxNativeKeyUtils . KeyTypes . User ,
56- description : $ "{ Constants . TokenCahceServiceName } :{ appId } ",
57- callout_info : IntPtr . Zero ,
58- dest_keyring : ( int ) LinuxNativeKeyUtils . KeyringType . KEY_SPEC_SESSION_KEYRING ) ;
62+ throw new ArgumentNullException ( string . Format (
63+ CultureInfo . CurrentCulture ,
64+ ErrorConstants . Message . NullOrEmptyParameter ,
65+ nameof ( appId ) ) ) ;
66+ }
5967
60- if ( key == - 1 )
61- LinuxNativeKeyUtils . add_key (
62- type : LinuxNativeKeyUtils . KeyTypes . User ,
63- description : $ "{ Constants . TokenCahceServiceName } :{ appId } ",
64- payload : encodedContent ,
65- plen : encodedContent . Length ,
66- keyring : ( int ) LinuxNativeKeyUtils . KeyringType . KEY_SPEC_SESSION_KEYRING ) ;
67- else
68- LinuxNativeKeyUtils . keyctl_update (
69- key : key ,
70- payload : encodedContent ,
71- plen : encodedContent . Length ) ;
68+ if ( plainContent == null || plainContent . Length == 0 )
69+ {
70+ return ;
71+ }
72+
73+ string encodedContent = Convert . ToBase64String ( plainContent ) ;
74+ int key = LinuxNativeKeyUtils . request_key (
75+ type : LinuxNativeKeyUtils . KeyTypes . User ,
76+ description : $ "{ Constants . TokenCacheServiceName } :{ appId } ",
77+ callout_info : IntPtr . Zero ,
78+ dest_keyring : ( int ) LinuxNativeKeyUtils . KeyringType . KEY_SPEC_SESSION_KEYRING ) ;
79+
80+ if ( key == - 1 )
81+ {
82+ LinuxNativeKeyUtils . add_key (
83+ type : LinuxNativeKeyUtils . KeyTypes . User ,
84+ description : $ "{ Constants . TokenCacheServiceName } :{ appId } ",
85+ payload : encodedContent ,
86+ plen : encodedContent . Length ,
87+ keyring : ( int ) LinuxNativeKeyUtils . KeyringType . KEY_SPEC_SESSION_KEYRING ) ;
88+ }
89+ else
90+ {
91+ LinuxNativeKeyUtils . keyctl_update (
92+ key : key ,
93+ payload : encodedContent ,
94+ plen : encodedContent . Length ) ;
7295 }
7396 }
7497
7598 /// <summary>
76- /// Deletes an app's token from Linux kerings faciility .
99+ /// Deletes an app's token from Linux keyrings facility .
77100 /// </summary>
78101 /// <param name="appId">An app/client id.</param>
79102 public static void DeleteToken ( string appId )
80103 {
104+ if ( string . IsNullOrEmpty ( appId ) )
105+ {
106+ throw new ArgumentNullException ( string . Format (
107+ CultureInfo . CurrentCulture ,
108+ ErrorConstants . Message . NullOrEmptyParameter ,
109+ nameof ( appId ) ) ) ;
110+ }
111+
81112 int key = LinuxNativeKeyUtils . request_key (
82113 type : LinuxNativeKeyUtils . KeyTypes . User ,
83- description : $ "{ Constants . TokenCahceServiceName } :{ appId } ",
114+ description : $ "{ Constants . TokenCacheServiceName } :{ appId } ",
84115 callout_info : IntPtr . Zero ,
85116 dest_keyring : ( int ) LinuxNativeKeyUtils . KeyringType . KEY_SPEC_SESSION_KEYRING ) ;
86117 if ( key != - 1 )
87118 {
88119 int removedState = LinuxNativeKeyUtils . keyctl_revoke ( key ) ;
89120 if ( removedState == - 1 )
121+ {
90122 throw new Exception ( "Failed to revoke token from cache." ) ;
123+ }
91124 }
92125 }
93126 }
0 commit comments