@@ -39,8 +39,12 @@ protected function getCodeFields($state = null)
39
39
{
40
40
$ fields = parent ::getCodeFields ($ state );
41
41
42
- $ fields ['user_scope ' ] = $ this ->formatScopes ($ this ->userScopes , $ this ->scopeSeparator );
43
- $ fields ['scope ' ] = $ this ->formatScopes ($ this ->scopes , $ this ->scopeSeparator );
42
+ if ($ this ->shouldUseOpenIdConnect ()) {
43
+ $ fields ['scope ' ] = $ this ->formatScopes ($ this ->userScopes , ' ' );
44
+ } else {
45
+ $ fields ['user_scope ' ] = $ this ->formatScopes ($ this ->userScopes , $ this ->scopeSeparator );
46
+ $ fields ['scope ' ] = $ this ->formatScopes ($ this ->scopes , $ this ->scopeSeparator );
47
+ }
44
48
45
49
return $ fields ;
46
50
}
@@ -57,7 +61,13 @@ public function user()
57
61
58
62
$ response = $ this ->getAccessTokenResponse ($ this ->getCode ());
59
63
60
- $ user = $ this ->getUserByToken (Arr::get ($ response , 'authed_user.access_token ' ));
64
+ if ($ this ->shouldUseOpenIdConnect ()) {
65
+ $ token = Arr::get ($ response , 'access_token ' );
66
+ } else {
67
+ $ token = Arr::get ($ response , 'authed_user.access_token ' );
68
+ }
69
+
70
+ $ user = $ this ->getUserByToken ($ token );
61
71
62
72
/** @var User $userInstance */
63
73
$ userInstance = $ this ->userInstance ($ response , $ user );
@@ -68,13 +78,25 @@ public function user()
68
78
69
79
protected function mapUserToObject (array $ user )
70
80
{
71
- return (new User )->setRaw ($ user )->map ([
72
- 'id ' => Arr::get ($ user , 'user.id ' ),
73
- 'name ' => Arr::get ($ user , 'user.name ' ),
74
- 'email ' => Arr::get ($ user , 'user.email ' ),
75
- 'avatar ' => Arr::get ($ user , 'user.image_512 ' ),
76
- 'organization_id ' => Arr::get ($ user , 'team.id ' ),
77
- ]);
81
+ if ($ this ->usesOpenIdScopes ()) {
82
+ $ attributes = [
83
+ 'id ' => Arr::get ($ user , 'sub ' ),
84
+ 'name ' => Arr::get ($ user , 'name ' ),
85
+ 'email ' => Arr::get ($ user , 'email ' ),
86
+ 'avatar ' => Arr::get ($ user , 'picture ' ),
87
+ 'organization_id ' => Arr::get ($ user , 'https://slack.com/team_id ' ),
88
+ ];
89
+ } else {
90
+ $ attributes = [
91
+ 'id ' => Arr::get ($ user , 'user.id ' ),
92
+ 'name ' => Arr::get ($ user , 'user.name ' ),
93
+ 'email ' => Arr::get ($ user , 'user.email ' ),
94
+ 'avatar ' => Arr::get ($ user , 'user.image_512 ' ),
95
+ 'organization_id ' => Arr::get ($ user , 'team.id ' ),
96
+ ];
97
+ }
98
+
99
+ return (new User )->setRaw ($ user )->map ($ attributes );
78
100
}
79
101
80
102
public function getAccessTokenResponse ($ code )
@@ -87,13 +109,23 @@ public function getAccessTokenResponse($code)
87
109
return json_decode ((string ) $ response ->getBody (), true );
88
110
}
89
111
90
- public function getAuthUrl ($ state )
112
+ public function getAuthUrl ($ state ): string
91
113
{
92
- return $ this ->buildAuthUrlFromBase ('https://slack.com/oauth/v2/authorize ' , $ state );
114
+ if ($ this ->shouldUseOpenIdConnect ()) {
115
+ $ url = 'https://slack.com/openid/connect/authorize ' ;
116
+ } else {
117
+ $ url = 'https://slack.com/oauth/v2/authorize ' ;
118
+ }
119
+
120
+ return $ this ->buildAuthUrlFromBase ($ url , $ state );
93
121
}
94
122
95
123
protected function getTokenUrl (): string
96
124
{
125
+ if ($ this ->shouldUseOpenIdConnect ()) {
126
+ return 'https://slack.com/api/openid.connect.token ' ;
127
+ }
128
+
97
129
return 'https://slack.com/api/oauth.v2.access ' ;
98
130
}
99
131
@@ -102,10 +134,32 @@ protected function getTokenUrl(): string
102
134
*/
103
135
protected function getUserByToken ($ token )
104
136
{
105
- $ response = $ this ->getHttpClient ()->get ('https://slack.com/api/users.identity ' , [
137
+ if ($ this ->usesOpenIdScopes ()) {
138
+ $ url = 'https://slack.com/api/openid.connect.userInfo ' ;
139
+ } else {
140
+ $ url = 'https://slack.com/api/users.identity ' ;
141
+ }
142
+
143
+ $ response = $ this ->getHttpClient ()->get ($ url , [
106
144
RequestOptions::HEADERS => ['Authorization ' => 'Bearer ' .$ token ],
107
145
]);
108
146
109
147
return json_decode ((string ) $ response ->getBody (), true );
110
148
}
149
+
150
+ protected function shouldUseOpenIdConnect (): bool
151
+ {
152
+ return $ this ->usesOpenIdScopes () && empty ($ this ->scopes );
153
+ }
154
+
155
+ protected function usesOpenIdScopes (): bool
156
+ {
157
+ $ openidScopes = ['openid ' , 'email ' , 'profile ' ];
158
+ $ identityScopes = ['identity.basic ' , 'identity.email ' , 'identity.team ' , 'identity.avatar ' ];
159
+
160
+ $ hasOpenIdScopes = ! empty (array_intersect ($ this ->userScopes , $ openidScopes ));
161
+ $ hasIdentityScopes = ! empty (array_intersect ($ this ->userScopes , $ identityScopes ));
162
+
163
+ return $ hasOpenIdScopes && ! $ hasIdentityScopes ;
164
+ }
111
165
}
0 commit comments