/
mattermost.ts
148 lines (146 loc) · 5.49 KB
/
mattermost.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* <div style={{backgroundColor: "#000", display: "flex", justifyContent: "space-between", color: "#fff", padding: 16}}>
* <span>Built-in <b>Mattermost</b> integration.</span>
* <a href="https://mattermost.com">
* <img style={{display: "block"}} src="https://authjs.dev/img/providers/mattermost.svg" height="48" width="48"/>
* </a>
* </div>
*
* @module providers/mattermost
*/
import type { OAuthConfig, OAuthUserConfig } from "./oauth.js"
/** [Get a user](https://api.mattermost.com/#tag/users/operation/GetUser) */
export interface MattermostProfile {
id: string
/** The time in milliseconds a user was created */
create_at: number
/** The time in milliseconds a user was last updated */
update_at: number
/** The time in milliseconds a user was deleted */
delete_at: number
username: string
auth_data: string
auth_service: string
email: string
email_verified: boolean
nickname: string
first_name: string
last_name: string
position: string
roles: string
notify_props: {
/** Set to "true" to enable channel-wide notifications (@channel, @all, etc.), "false" to disable. Defaults to "true". */
channel: string
comments: string
/** Set to "all" to receive desktop notifications for all activity, "mention" for mentions and direct messages only, and "none" to disable. Defaults to "all". */
desktop: string
/** Set to "true" to enable sound on desktop notifications, "false" to disable. Defaults to "true". */
desktop_sound: string
desktop_threads: string
/** Set to "true" to enable email notifications, "false" to disable. Defaults to "true". */
email: string
email_threads: string
/** Set to "true" to enable mentions for first name. Defaults to "true" if a first name is set, "false" otherwise. */
first_name: string
/** A comma-separated list of words to count as mentions. Defaults to username and @username. */
mention_keys: string
/** Set to "all" to receive push notifications for all activity, "mention" for mentions and direct messages only, and "none" to disable. Defaults to "mention". */
push: string
push_status: string
push_threads: string
}
last_password_update: number
locale: string
timezone: {
/** This value is set automatically when the "useAutomaticTimezone" is set to "true". */
automaticTimezone: string
/** Value when setting manually the timezone, i.e. "Europe/Berlin". */
manualTimezone: string
/** Set to "true" to use the browser/system timezone, "false" to set manually. Defaults to "true". */
useAutomaticTimezone: string
}
disable_welcome_email: boolean
/** ID of accepted terms of service, if any. This field is not present if empty. */
terms_of_service_id?: string
/** The time in milliseconds the user accepted the terms of service */
terms_of_service_create_at?: number
}
/**
* Add Mattermost login to your page.
*
* ### Setup
*
* #### Callback URL
* ```
* https://example.com/api/auth/callback/mattermost
* ```
*
* #### Configuration
*```js
* import Auth from "@auth/core"
* import Mattermost from "@auth/core/providers/mattermost"
*
* const request = new Request(origin)
* const response = await Auth(request, {
* providers: [Mattermost({ clientId: MATTERMOST_CLIENT_ID, clientSecret: MATTERMOST_CLIENT_SECRET, issuer: MATTERMOST_ISSUER // The base url of your Mattermost instance. e.g `https://my-cool-server.cloud.mattermost.com` })],
* })
* ```
*
* ### Resources
*
* - [Mattermost OAuth documentation](https://example.com)
*
* ### Notes
*
* By default, Auth.js assumes that the Mattermost provider is
* based on the [OAuth 2](https://www.rfc-editor.org/rfc/rfc6749.html) specification.
*
* To create your Mattermost OAuth2 app visit `http://<your Mattermost instance url>/<your team>/integrations/oauth2-apps`
*
* :::warning
*
* The Mattermost provider requires the `issuer` option to be set. This is the base url of your Mattermost instance. e.g https://my-cool-server.cloud.mattermost.com
*
* :::
*
* :::tip
*
* The Mattermost provider comes with a [default configuration](https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/mattermost.ts).
* To override the defaults for your use case, check out [customizing a built-in OAuth provider](https://authjs.dev/guides/configuring-oauth-providers).
*
* :::
*
* :::info **Disclaimer**
*
* If you think you found a bug in the default configuration, you can [open an issue](https://authjs.dev/new/provider-issue).
*
* Auth.js strictly adheres to the specification and it cannot take responsibility for any deviation from
* the spec by the provider. You can open an issue, but if the problem is non-compliance with the spec,
* we might not pursue a resolution. You can ask for more help in [Discussions](https://authjs.dev/new/github-discussions).
*
* :::
*/
export default function Mattermost<P extends MattermostProfile>(
config: OAuthUserConfig<P> & { issuer: string }
): OAuthConfig<P> {
const { issuer, ...rest } = config
return {
id: "mattermost",
name: "Mattermost",
type: "oauth",
client: { token_endpoint_auth_method: "client_secret_post" },
token: `${issuer}/oauth/access_token`,
authorization: `${issuer}/oauth/authorize`,
userinfo: `${issuer}/api/v4/users/me`,
profile(profile) {
return {
id: profile.id,
name: profile.username ?? `${profile.first_name} ${profile.last_name}`,
email: profile.email,
image: null,
}
},
style: { bg: "#000", text: "#fff" },
options: rest,
}
}