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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Authentication configuration for Slack API
use crate::error::{Result, SlackError};
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, COOKIE, USER_AGENT};
/// Authentication configuration
#[derive(Clone, Debug)]
pub struct AuthConfig {
pub(crate) auth_type: AuthType,
}
/// Authentication type
#[derive(Clone, Debug)]
pub enum AuthType {
/// Stealth mode using xoxc token and xoxd cookie
/// This uses browser-extracted tokens without requiring bot installation
Stealth {
xoxc_token: String,
xoxd_cookie: String,
},
/// OAuth user token (xoxp-)
OAuth { token: String },
/// Bot token (xoxb-)
Bot { token: String },
}
impl AuthConfig {
/// Create a stealth mode authentication configuration
///
/// # Arguments
///
/// * `xoxc_token` - The xoxc token from browser
/// * `xoxd_cookie` - The d cookie value from browser
///
/// # Example
///
/// ```
/// use slacko::AuthConfig;
///
/// let auth = AuthConfig::stealth("xoxc-123...", "xoxd-456...");
/// ```
pub fn stealth(xoxc_token: impl Into<String>, xoxd_cookie: impl Into<String>) -> Self {
Self {
auth_type: AuthType::Stealth {
xoxc_token: xoxc_token.into(),
xoxd_cookie: xoxd_cookie.into(),
},
}
}
/// Create an OAuth token authentication configuration
///
/// # Arguments
///
/// * `token` - The OAuth user token (starts with xoxp-)
///
/// # Example
///
/// ```
/// use slacko::AuthConfig;
///
/// let auth = AuthConfig::oauth("xoxp-123...");
/// ```
pub fn oauth(token: impl Into<String>) -> Self {
Self {
auth_type: AuthType::OAuth {
token: token.into(),
},
}
}
/// Create a bot token authentication configuration
///
/// # Arguments
///
/// * `token` - The bot token (starts with xoxb-)
///
/// # Example
///
/// ```
/// use slacko::AuthConfig;
///
/// let auth = AuthConfig::bot("xoxb-123...");
/// ```
pub fn bot(token: impl Into<String>) -> Self {
Self {
auth_type: AuthType::Bot {
token: token.into(),
},
}
}
/// Load authentication from environment variables
///
/// Checks for the following environment variables in order:
/// 1. `SLACK_XOXC_TOKEN` and `SLACK_XOXD_COOKIE` for stealth mode
/// 2. `SLACK_XOXP_TOKEN` for OAuth
/// 3. `SLACK_BOT_TOKEN` or `SLACK_TOKEN` for bot tokens
///
/// # Example
///
/// ```no_run
/// use slacko::AuthConfig;
///
/// let auth = AuthConfig::from_env().expect("No Slack credentials found");
/// ```
pub fn from_env() -> Result<Self> {
// Try stealth mode first
if let (Ok(xoxc), Ok(xoxd)) = (
std::env::var("SLACK_XOXC_TOKEN"),
std::env::var("SLACK_XOXD_COOKIE"),
) {
return Ok(Self::stealth(xoxc, xoxd));
}
// Try OAuth token
if let Ok(token) = std::env::var("SLACK_XOXP_TOKEN") {
return Ok(Self::oauth(token));
}
// Try bot token
if let Ok(token) =
std::env::var("SLACK_BOT_TOKEN").or_else(|_| std::env::var("SLACK_TOKEN"))
{
return Ok(Self::bot(token));
}
Err(SlackError::config_error(
"No Slack credentials found in environment. Set SLACK_XOXC_TOKEN + SLACK_XOXD_COOKIE, SLACK_XOXP_TOKEN, or SLACK_BOT_TOKEN",
))
}
/// Build HTTP headers for API requests
pub(crate) fn build_headers(&self) -> HeaderMap {
let mut headers = HeaderMap::new();
match &self.auth_type {
AuthType::Stealth {
xoxc_token,
xoxd_cookie,
} => {
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", xoxc_token))
.unwrap_or_else(|_| HeaderValue::from_static("")),
);
headers.insert(
COOKIE,
HeaderValue::from_str(&format!("d={}", xoxd_cookie))
.unwrap_or_else(|_| HeaderValue::from_static("")),
);
}
AuthType::OAuth { token } | AuthType::Bot { token } => {
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", token))
.unwrap_or_else(|_| HeaderValue::from_static("")),
);
}
}
headers.insert(USER_AGENT, HeaderValue::from_static("slack-sdk-rust/0.1.0"));
headers
}
/// Get the authentication type as a string
pub fn auth_type_str(&self) -> &str {
match &self.auth_type {
AuthType::Stealth { .. } => "stealth",
AuthType::OAuth { .. } => "oauth",
AuthType::Bot { .. } => "bot",
}
}
}